Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rewrote ObjectCache.php to conform to the modern coding style, and …
…to be less convoluted about how CACHE_ANYTHING and CACHE_ACCEL are resolved. Moved most functionality to static members of a new ObjectCache class. * Moved the global functions to GlobalFunctions.php, where they are now just convenience wrappers. Made them return non-references. Updated callers (none found in extensions). * Added an advanced configuration method, $wgObjectCaches, which allows a lot more detail in the object cache configuration than $wgMainCacheType. * Made all object cache classes derive from BagOStuff. * Split the MWMemcached class into a generic client class and a MediaWiki-specific wrapper class. The wrapper class presents a simple BagOStuff interface to calling code, hiding memcached client internals, and will simplify the task of supporting the PECL extension. * Added some extra constructor parameters to MWMemcached, configurable via $wgObjectCaches. * Removed the *_multi() methods from BagOStuff, my grepping indicates that they are not used. * Rewrote FakeMemCachedClient as a BagOStuff subclass, called EmptyBagOStuff. * Added an optional "server" parameter to SQLBagOStuff. This allows the server holding the objectcache table to be different from the server holding the core DB. * Added MultiWriteBagOStuff: a cache class which writes to multiple locations, and reads from them in a defined fallback sequence. This can be used to extend the cache space by adding disk-backed storage to existing in-memory caches. * Made MWMemcached::get() return false on failure instead of null, to match the BagOStuff documentation and the other BagOStuff subclasses. Anything that was relying on it returning null would have already been broken with SqlBagOStuff. * Fixed a bug in the memcached client causing keys with spaces or line breaks in them to break the memcached protocol, injecting arbitrary commands or parameters. Since the PECL client apparently also has this flaw, I implemented the fix in the wrapper class. * Renamed BagOStuff::set_debug() to setDebug(), since we aren't emulating the memcached client anymore * Fixed spelling error in MWMemcached: persistant -> persistent
- Loading branch information
Tim Starling
committed
Mar 3, 2011
1 parent
a9bba54
commit be76d86
Showing
14 changed files
with
427 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/** | ||
* A BagOStuff object with no objects in it. Used to provide a no-op object to calling code. | ||
* | ||
* @ingroup Cache | ||
*/ | ||
class EmptyBagOStuff extends BagOStuff { | ||
function get( $key ) { | ||
return false; | ||
} | ||
|
||
function set( $key, $value, $exp = 0 ) { | ||
return true; | ||
} | ||
|
||
function delete( $key, $time = 0 ) { | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* Backwards compatibility alias for EmptyBagOStuff | ||
* @deprecated | ||
*/ | ||
class FakeMemCachedClient extends EmptyBagOStuff { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
/** | ||
* A wrapper class for the pure-PHP memcached client, exposing a BagOStuff interface. | ||
*/ | ||
class MemcachedPhpBagOStuff extends BagOStuff { | ||
/** | ||
* Constructor. | ||
* | ||
* Available parameters are: | ||
* - servers: The list of IP:port combinations holding the memcached servers. | ||
* - debug: Whether to set the debug flag in the underlying client. | ||
* - persistent: Whether to use a persistent connection | ||
* - compress_threshold: The minimum size an object must be before it is compressed | ||
* - timeout: The read timeout in microseconds | ||
* - connect_timeout: The connect timeout in seconds | ||
*/ | ||
function __construct( $params ) { | ||
if ( !isset( $params['servers'] ) ) { | ||
$params['servers'] = $GLOBALS['wgMemCachedServers']; | ||
} | ||
if ( !isset( $params['debug'] ) ) { | ||
$params['debug'] = $GLOBALS['wgMemCachedDebug']; | ||
} | ||
if ( !isset( $params['persistent'] ) ) { | ||
$params['persistent'] = $GLOBALS['wgMemCachedPersistent']; | ||
} | ||
if ( !isset( $params['compress_threshold'] ) ) { | ||
$params['compress_threshold'] = 1500; | ||
} | ||
if ( !isset( $params['timeout'] ) ) { | ||
$params['timeout'] = $GLOBALS['wgMemCachedTimeout']; | ||
} | ||
if ( !isset( $params['connect_timeout'] ) ) { | ||
$params['connect_timeout'] = 0.1; | ||
} | ||
|
||
$this->client = new MemCachedClientforWiki( $params ); | ||
$this->client->set_servers( $params['servers'] ); | ||
$this->client->set_debug( $params['debug'] ); | ||
} | ||
|
||
public function setDebug( $debug ) { | ||
$this->client->set_debug( $debug ); | ||
} | ||
|
||
public function get( $key ) { | ||
return $this->client->get( $this->encodeKey( $key ) ); | ||
} | ||
|
||
public function set( $key, $value, $exptime = 0 ) { | ||
return $this->client->set( $this->encodeKey( $key ), $value, $exptime ); | ||
} | ||
|
||
public function delete( $key, $time = 0 ) { | ||
return $this->client->delete( $this->encodeKey( $key ), $time ); | ||
} | ||
|
||
public function lock( $key, $timeout = 0 ) { | ||
return $this->client->lock( $this->encodeKey( $key ), $timeout ); | ||
} | ||
|
||
public function unlock( $key ) { | ||
return $this->client->unlock( $this->encodeKey( $key ) ); | ||
} | ||
|
||
public function add( $key, $value, $exptime = 0 ) { | ||
return $this->client->add( $this->encodeKey( $key ), $value, $exptime ); | ||
} | ||
|
||
public function replace( $key, $value, $exptime = 0 ) { | ||
return $this->client->replace( $this->encodeKey( $key ), $value, $exptime ); | ||
} | ||
|
||
public function incr( $key, $value = 1 ) { | ||
return $this->client->incr( $this->encodeKey( $key ), $value ); | ||
} | ||
|
||
public function decr( $key, $value = 1 ) { | ||
return $this->client->decr( $this->encodeKey( $key ), $value ); | ||
} | ||
|
||
/** | ||
* Get the underlying client object. This is provided for debugging | ||
* purposes. | ||
*/ | ||
public function getClient() { | ||
return $this->client; | ||
} | ||
|
||
/** | ||
* Encode a key for use on the wire inside the memcached protocol. | ||
* | ||
* We encode spaces and line breaks to avoid protocol errors. We encode | ||
* the other control characters for compatibility with libmemcached | ||
* verify_key. We leave other punctuation alone, to maximise backwards | ||
* compatibility. | ||
*/ | ||
public function encodeKey( $key ) { | ||
return preg_replace_callback( '/[\x00-\x20\x25\x7f]+/', | ||
array( $this, 'encodeKeyCallback' ), $key ); | ||
} | ||
|
||
protected function encodeKeyCallback( $m ) { | ||
return urlencode( $m[0] ); | ||
} | ||
|
||
/** | ||
* Decode a key encoded with encodeKey(). This is provided as a convenience | ||
* function for debugging. | ||
*/ | ||
public function decodeKey( $key ) { | ||
return urldecode( $key ); | ||
} | ||
} | ||
|
Oops, something went wrong.