| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace Wikimedia\ObjectCache; |
| 21 |
|
| 22 |
use InvalidArgumentException; |
| 23 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
class MultiWriteBagOStuff extends BagOStuff { |
| 104 |
|
| 105 |
protected $caches; |
| 106 |
|
| 107 |
|
| 108 |
protected $asyncWrites = false; |
| 109 |
|
| 110 |
protected $cacheIndexes = []; |
| 111 |
|
| 112 |
|
| 113 |
private static $UPGRADE_TTL = 3600; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function __construct( $params ) { |
| 138 |
parent::__construct( $params ); |
| 139 |
|
| 140 |
if ( empty( $params['caches'] ) || !is_array( $params['caches'] ) ) { |
| 141 |
throw new InvalidArgumentException( |
| 142 |
__METHOD__ . ': "caches" parameter must be an array of caches' |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
$this->caches = []; |
| 147 |
foreach ( $params['caches'] as $cacheInfo ) { |
| 148 |
if ( $cacheInfo instanceof BagOStuff ) { |
| 149 |
$this->caches[] = $cacheInfo; |
| 150 |
} else { |
| 151 |
$this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo ); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
$this->attrMap = $this->mergeFlagMaps( $this->caches ); |
| 156 |
|
| 157 |
$this->asyncWrites = ( |
| 158 |
isset( $params['replication'] ) && |
| 159 |
$params['replication'] === 'async' && |
| 160 |
is_callable( $this->asyncHandler ) |
| 161 |
); |
| 162 |
|
| 163 |
$this->cacheIndexes = array_keys( $this->caches ); |
| 164 |
} |
| 165 |
|
| 166 |
public function get( $key, $flags = 0 ) { |
| 167 |
$args = func_get_args(); |
| 168 |
|
| 169 |
if ( $this->fieldHasFlags( $flags, self::READ_LATEST ) ) { |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
return $this->callKeyMethodOnTierCache( |
| 174 |
0, |
| 175 |
__FUNCTION__, |
| 176 |
self::ARG0_KEY, |
| 177 |
self::RES_NONKEY, |
| 178 |
$args |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
$value = false; |
| 183 |
|
| 184 |
$missIndexes = []; |
| 185 |
foreach ( $this->cacheIndexes as $i ) { |
| 186 |
$value = $this->callKeyMethodOnTierCache( |
| 187 |
$i, |
| 188 |
__FUNCTION__, |
| 189 |
self::ARG0_KEY, |
| 190 |
self::RES_NONKEY, |
| 191 |
$args |
| 192 |
); |
| 193 |
if ( $value !== false ) { |
| 194 |
break; |
| 195 |
} |
| 196 |
$missIndexes[] = $i; |
| 197 |
} |
| 198 |
|
| 199 |
if ( |
| 200 |
$value !== false && |
| 201 |
$this->fieldHasFlags( $flags, self::READ_VERIFIED ) && |
| 202 |
$missIndexes |
| 203 |
) { |
| 204 |
|
| 205 |
$this->callKeyWriteMethodOnTierCaches( |
| 206 |
$missIndexes, |
| 207 |
'set', |
| 208 |
self::ARG0_KEY, |
| 209 |
self::RES_NONKEY, |
| 210 |
[ $key, $value, self::$UPGRADE_TTL ] |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
return $value; |
| 215 |
} |
| 216 |
|
| 217 |
public function set( $key, $value, $exptime = 0, $flags = 0 ) { |
| 218 |
return $this->callKeyWriteMethodOnTierCaches( |
| 219 |
$this->cacheIndexes, |
| 220 |
__FUNCTION__, |
| 221 |
self::ARG0_KEY, |
| 222 |
self::RES_NONKEY, |
| 223 |
func_get_args() |
| 224 |
); |
| 225 |
} |
| 226 |
|
| 227 |
public function delete( $key, $flags = 0 ) { |
| 228 |
return $this->callKeyWriteMethodOnTierCaches( |
| 229 |
$this->cacheIndexes, |
| 230 |
__FUNCTION__, |
| 231 |
self::ARG0_KEY, |
| 232 |
self::RES_NONKEY, |
| 233 |
func_get_args() |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
public function add( $key, $value, $exptime = 0, $flags = 0 ) { |
| 238 |
|
| 239 |
$ok = $this->callKeyMethodOnTierCache( |
| 240 |
0, |
| 241 |
__FUNCTION__, |
| 242 |
self::ARG0_KEY, |
| 243 |
self::RES_NONKEY, |
| 244 |
func_get_args() |
| 245 |
); |
| 246 |
|
| 247 |
if ( $ok ) { |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
$okSecondaries = $this->callKeyWriteMethodOnTierCaches( |
| 252 |
array_slice( $this->cacheIndexes, 1 ), |
| 253 |
'set', |
| 254 |
self::ARG0_KEY, |
| 255 |
self::RES_NONKEY, |
| 256 |
[ $key, $value, $exptime, $flags ] |
| 257 |
); |
| 258 |
if ( $okSecondaries === false ) { |
| 259 |
$ok = false; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
return $ok; |
| 264 |
} |
| 265 |
|
| 266 |
public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) { |
| 267 |
return $this->callKeyWriteMethodOnTierCaches( |
| 268 |
$this->cacheIndexes, |
| 269 |
__FUNCTION__, |
| 270 |
self::ARG0_KEY, |
| 271 |
self::RES_NONKEY, |
| 272 |
func_get_args() |
| 273 |
); |
| 274 |
} |
| 275 |
|
| 276 |
public function changeTTL( $key, $exptime = 0, $flags = 0 ) { |
| 277 |
return $this->callKeyWriteMethodOnTierCaches( |
| 278 |
$this->cacheIndexes, |
| 279 |
__FUNCTION__, |
| 280 |
self::ARG0_KEY, |
| 281 |
self::RES_NONKEY, |
| 282 |
func_get_args() |
| 283 |
); |
| 284 |
} |
| 285 |
|
| 286 |
public function lock( $key, $timeout = 6, $exptime = 6, $rclass = '' ) { |
| 287 |
|
| 288 |
return $this->callKeyMethodOnTierCache( |
| 289 |
0, |
| 290 |
__FUNCTION__, |
| 291 |
self::ARG0_KEY, |
| 292 |
self::RES_NONKEY, |
| 293 |
func_get_args() |
| 294 |
); |
| 295 |
} |
| 296 |
|
| 297 |
public function unlock( $key ) { |
| 298 |
|
| 299 |
return $this->callKeyMethodOnTierCache( |
| 300 |
0, |
| 301 |
__FUNCTION__, |
| 302 |
self::ARG0_KEY, |
| 303 |
self::RES_NONKEY, |
| 304 |
func_get_args() |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
public function deleteObjectsExpiringBefore( |
| 309 |
$timestamp, |
| 310 |
?callable $progress = null, |
| 311 |
$limit = INF, |
| 312 |
?string $tag = null |
| 313 |
) { |
| 314 |
$ret = false; |
| 315 |
foreach ( $this->caches as $cache ) { |
| 316 |
if ( $cache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit, $tag ) ) { |
| 317 |
$ret = true; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
return $ret; |
| 322 |
} |
| 323 |
|
| 324 |
public function getMulti( array $keys, $flags = 0 ) { |
| 325 |
|
| 326 |
$res = []; |
| 327 |
foreach ( $keys as $key ) { |
| 328 |
$val = $this->get( $key, $flags ); |
| 329 |
if ( $val !== false ) { |
| 330 |
$res[$key] = $val; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
return $res; |
| 335 |
} |
| 336 |
|
| 337 |
public function setMulti( array $valueByKey, $exptime = 0, $flags = 0 ) { |
| 338 |
return $this->callKeyWriteMethodOnTierCaches( |
| 339 |
$this->cacheIndexes, |
| 340 |
__FUNCTION__, |
| 341 |
self::ARG0_KEYMAP, |
| 342 |
self::RES_NONKEY, |
| 343 |
func_get_args() |
| 344 |
); |
| 345 |
} |
| 346 |
|
| 347 |
public function deleteMulti( array $keys, $flags = 0 ) { |
| 348 |
return $this->callKeyWriteMethodOnTierCaches( |
| 349 |
$this->cacheIndexes, |
| 350 |
__FUNCTION__, |
| 351 |
self::ARG0_KEYARR, |
| 352 |
self::RES_NONKEY, |
| 353 |
func_get_args() |
| 354 |
); |
| 355 |
} |
| 356 |
|
| 357 |
public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) { |
| 358 |
return $this->callKeyWriteMethodOnTierCaches( |
| 359 |
$this->cacheIndexes, |
| 360 |
__FUNCTION__, |
| 361 |
self::ARG0_KEYARR, |
| 362 |
self::RES_NONKEY, |
| 363 |
func_get_args() |
| 364 |
); |
| 365 |
} |
| 366 |
|
| 367 |
public function incrWithInit( $key, $exptime, $step = 1, $init = null, $flags = 0 ) { |
| 368 |
return $this->callKeyWriteMethodOnTierCaches( |
| 369 |
$this->cacheIndexes, |
| 370 |
__FUNCTION__, |
| 371 |
self::ARG0_KEY, |
| 372 |
self::RES_NONKEY, |
| 373 |
func_get_args() |
| 374 |
); |
| 375 |
} |
| 376 |
|
| 377 |
public function setMockTime( &$time ) { |
| 378 |
parent::setMockTime( $time ); |
| 379 |
foreach ( $this->caches as $cache ) { |
| 380 |
$cache->setMockTime( $time ); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
private function callKeyMethodOnTierCache( $index, $method, $arg0Sig, $rvSig, array $args ) { |
| 396 |
return $this->caches[$index]->proxyCall( $method, $arg0Sig, $rvSig, $args, $this ); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
private function callKeyWriteMethodOnTierCaches( |
| 411 |
array $indexes, |
| 412 |
$method, |
| 413 |
$arg0Sig, |
| 414 |
$resSig, |
| 415 |
array $args |
| 416 |
) { |
| 417 |
$res = null; |
| 418 |
|
| 419 |
if ( $this->asyncWrites && array_diff( $indexes, [ 0 ] ) && $method !== 'merge' ) { |
| 420 |
|
| 421 |
|
| 422 |
$args = unserialize( serialize( $args ) ); |
| 423 |
} |
| 424 |
|
| 425 |
foreach ( $indexes as $i ) { |
| 426 |
$cache = $this->caches[$i]; |
| 427 |
|
| 428 |
if ( $i == 0 || !$this->asyncWrites ) { |
| 429 |
|
| 430 |
$storeRes = $cache->proxyCall( $method, $arg0Sig, $resSig, $args, $this ); |
| 431 |
if ( $storeRes === false ) { |
| 432 |
$res = false; |
| 433 |
} elseif ( $res === null ) { |
| 434 |
|
| 435 |
$res = $storeRes; |
| 436 |
} |
| 437 |
} else { |
| 438 |
|
| 439 |
( $this->asyncHandler )( |
| 440 |
function () use ( $cache, $method, $arg0Sig, $resSig, $args ) { |
| 441 |
$cache->proxyCall( $method, $arg0Sig, $resSig, $args, $this ); |
| 442 |
} |
| 443 |
); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
return $res; |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
class_alias( MultiWriteBagOStuff::class, 'MultiWriteBagOStuff' ); |