23use Wikimedia\ObjectFactory;
66 if ( empty(
$params[
'caches'] ) || !is_array(
$params[
'caches'] ) ) {
67 throw new InvalidArgumentException(
68 __METHOD__ .
': "caches" parameter must be an array of caches'
73 foreach (
$params[
'caches'] as $cacheInfo ) {
75 $this->caches[] = $cacheInfo;
77 if ( !isset( $cacheInfo[
'args'] ) ) {
82 $cacheInfo[
'args'] = [ $cacheInfo ];
84 $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
89 $this->asyncWrites = (
90 isset(
$params[
'replication'] ) &&
91 $params[
'replication'] ===
'async' &&
92 is_callable( $this->asyncHandler )
97 foreach ( $this->caches as
$cache ) {
102 protected function doGet( $key, $flags = 0 ) {
103 if ( ( $flags & self::READ_LATEST ) == self::READ_LATEST ) {
107 return $this->caches[0]->get( $key, $flags );
112 foreach ( $this->caches as
$cache ) {
122 && ( $flags & self::READ_VERIFIED ) == self::READ_VERIFIED
124 $this->
doWrite( $misses, $this->asyncWrites,
'set', $key,
$value, self::UPGRADE_TTL );
130 public function set( $key,
$value, $exptime = 0, $flags = 0 ) {
138 public function delete( $key ) {
139 return $this->
doWrite( self::ALL, $this->asyncWrites,
'delete', $key );
143 return $this->
doWrite( self::ALL, $this->asyncWrites,
'add', $key,
$value, $exptime );
147 return $this->
doWrite( self::ALL, $this->asyncWrites,
'incr', $key,
$value );
151 return $this->
doWrite( self::ALL, $this->asyncWrites,
'decr', $key,
$value );
154 public function lock( $key, $timeout = 6, $expiry = 6, $rclass =
'' ) {
156 return $this->caches[0]->lock( $key, $timeout, $expiry, $rclass );
161 return $this->caches[0]->unlock( $key );
165 return $this->caches[0]->getLastError();
169 $this->caches[0]->clearLastError();
183 $args = array_slice( func_get_args(), 3 );
191 foreach ( $this->caches as $i =>
$cache ) {
192 if ( $i >= $count ) {
198 if ( !call_user_func_array( [
$cache, $method ],
$args ) ) {
207 if ( !call_user_func_array( [
$cache, $method ],
$args ) ) {
208 $logger->warning(
"Async $method op failed" );
228 foreach ( $this->caches as
$cache ) {
229 if (
$cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
237 public function makeKey( $class, $component =
null ) {
238 return call_user_func_array( [ $this->caches[0], __FUNCTION__ ], func_get_args() );
242 return call_user_func_array( [ $this->caches[0], __FUNCTION__ ], func_get_args() );
unserialize( $serialized)
interface is intended to be more or less compatible with the PHP memcached client.
const WRITE_SYNC
Bitfield constants for set()/merge()
mergeFlagMaps(array $bags)
Merge the flag maps of one or more BagOStuff objects into a "lowest common denominator" map.
A cache class that replicates all writes to multiple child caches.
doWrite( $count, $asyncWrites, $method)
Apply a write method to the first $count backing caches.
add( $key, $value, $exptime=0)
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
__construct( $params)
$params include:
clearLastError()
Clear the "last error" registry.
incr( $key, $value=1)
Increase stored value of $key by $value while preserving its TTL.
makeGlobalKey( $class, $component=null)
Make a global cache key.
bool $asyncWrites
Use async secondary writes.
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
const ALL
Idiom for "write to all backends".
makeKey( $class, $component=null)
Make a cache key, scoped to this instance's keyspace.
deleteObjectsExpiringBefore( $date, $progressCallback=false)
Delete objects expiring before a certain date.
decr( $key, $value=1)
Decrease stored value of $key by $value while preserving its TTL.
unlock( $key)
Release an advisory lock on a key string.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
processing should stop and the error should be shown to the user * false