MediaWiki REL1_30
MultiWriteBagOStuff.php
Go to the documentation of this file.
1<?php
33 protected $caches;
35 protected $asyncWrites = false;
36
38 const ALL = INF;
39
40 const UPGRADE_TTL = 3600; // TTL when a key is copied to a higher cache tier
41
62 public function __construct( $params ) {
63 parent::__construct( $params );
64
65 if ( empty( $params['caches'] ) || !is_array( $params['caches'] ) ) {
66 throw new InvalidArgumentException(
67 __METHOD__ . ': "caches" parameter must be an array of caches'
68 );
69 }
70
71 $this->caches = [];
72 foreach ( $params['caches'] as $cacheInfo ) {
73 if ( $cacheInfo instanceof BagOStuff ) {
74 $this->caches[] = $cacheInfo;
75 } else {
76 if ( !isset( $cacheInfo['args'] ) ) {
77 // B/C for when $cacheInfo was for ObjectCache::newFromParams().
78 // Callers intenting this to be for ObjectFactory::getObjectFromSpec
79 // should have set "args" per the docs above. Doings so avoids extra
80 // (likely harmless) params (factory/class/calls) ending up in "args".
81 $cacheInfo['args'] = [ $cacheInfo ];
82 }
83 $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
84 }
85 }
86 $this->mergeFlagMaps( $this->caches );
87
88 $this->asyncWrites = (
89 isset( $params['replication'] ) &&
90 $params['replication'] === 'async' &&
91 is_callable( $this->asyncHandler )
92 );
93 }
94
95 public function setDebug( $debug ) {
96 foreach ( $this->caches as $cache ) {
97 $cache->setDebug( $debug );
98 }
99 }
100
101 protected function doGet( $key, $flags = 0 ) {
102 if ( ( $flags & self::READ_LATEST ) == self::READ_LATEST ) {
103 // If the latest write was a delete(), we do NOT want to fallback
104 // to the other tiers and possibly see the old value. Also, this
105 // is used by mergeViaLock(), which only needs to hit the primary.
106 return $this->caches[0]->get( $key, $flags );
107 }
108
109 $misses = 0; // number backends checked
110 $value = false;
111 foreach ( $this->caches as $cache ) {
112 $value = $cache->get( $key, $flags );
113 if ( $value !== false ) {
114 break;
115 }
116 ++$misses;
117 }
118
119 if ( $value !== false
120 && $misses > 0
121 && ( $flags & self::READ_VERIFIED ) == self::READ_VERIFIED
122 ) {
123 $this->doWrite( $misses, $this->asyncWrites, 'set', $key, $value, self::UPGRADE_TTL );
124 }
125
126 return $value;
127 }
128
129 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
130 $asyncWrites = ( ( $flags & self::WRITE_SYNC ) == self::WRITE_SYNC )
131 ? false
133
134 return $this->doWrite( self::ALL, $asyncWrites, 'set', $key, $value, $exptime );
135 }
136
137 public function delete( $key ) {
138 return $this->doWrite( self::ALL, $this->asyncWrites, 'delete', $key );
139 }
140
141 public function add( $key, $value, $exptime = 0 ) {
142 return $this->doWrite( self::ALL, $this->asyncWrites, 'add', $key, $value, $exptime );
143 }
144
145 public function incr( $key, $value = 1 ) {
146 return $this->doWrite( self::ALL, $this->asyncWrites, 'incr', $key, $value );
147 }
148
149 public function decr( $key, $value = 1 ) {
150 return $this->doWrite( self::ALL, $this->asyncWrites, 'decr', $key, $value );
151 }
152
153 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
154 // Only need to lock the first cache; also avoids deadlocks
155 return $this->caches[0]->lock( $key, $timeout, $expiry, $rclass );
156 }
157
158 public function unlock( $key ) {
159 // Only the first cache is locked
160 return $this->caches[0]->unlock( $key );
161 }
162
163 public function getLastError() {
164 return $this->caches[0]->getLastError();
165 }
166
167 public function clearLastError() {
168 $this->caches[0]->clearLastError();
169 }
170
180 protected function doWrite( $count, $asyncWrites, $method /*, ... */ ) {
181 $ret = true;
182 $args = array_slice( func_get_args(), 3 );
183
184 if ( $count > 1 && $asyncWrites ) {
185 // Deep-clone $args to prevent misbehavior when something writes an
186 // object to the BagOStuff then modifies it afterwards, e.g. T168040.
188 }
189
190 foreach ( $this->caches as $i => $cache ) {
191 if ( $i >= $count ) {
192 break; // ignore the lower tiers
193 }
194
195 if ( $i == 0 || !$asyncWrites ) {
196 // First store or in sync mode: write now and get result
197 if ( !call_user_func_array( [ $cache, $method ], $args ) ) {
198 $ret = false;
199 }
200 } else {
201 // Secondary write in async mode: do not block this HTTP request
203 call_user_func(
204 $this->asyncHandler,
205 function () use ( $cache, $method, $args, $logger ) {
206 if ( !call_user_func_array( [ $cache, $method ], $args ) ) {
207 $logger->warning( "Async $method op failed" );
208 }
209 }
210 );
211 }
212 }
213
214 return $ret;
215 }
216
225 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
226 $ret = false;
227 foreach ( $this->caches as $cache ) {
228 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
229 $ret = true;
230 }
231 }
232
233 return $ret;
234 }
235
236 public function makeKey() {
237 return call_user_func_array( [ $this->caches[0], __FUNCTION__ ], func_get_args() );
238 }
239
240 public function makeGlobalKey() {
241 return call_user_func_array( [ $this->caches[0], __FUNCTION__ ], func_get_args() );
242 }
243}
serialize()
unserialize( $serialized)
if( $line===false) $args
Definition cdb.php:63
interface is intended to be more or less compatible with the PHP memcached client.
Definition BagOStuff.php:47
const WRITE_SYNC
Bitfield constants for set()/merge()
Definition BagOStuff.php:86
mergeFlagMaps(array $bags)
Merge the flag maps of one or more BagOStuff objects into a "lowest common denominator" map.
LoggerInterface $logger
Definition BagOStuff.php:55
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.
makeGlobalKey()
Make a global cache key.
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.
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".
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.
makeKey()
Make a cache key, scoped to this instance's keyspace.
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition hooks.txt:2805
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
Definition hooks.txt:1975
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
$debug
Definition mcc.php:31
$cache
Definition mcc.php:33
$params