MediaWiki master
WANGetWithSetCallbackBuilder.php
Go to the documentation of this file.
1<?php
8
9use LogicException;
10
58 private $callback = null;
59 private WANObjectCache $cache;
61 private ?string $key = null;
63 private ?int $lifetime = null;
65 private array $options = [];
66
71 public function __construct( WANObjectCache $cache ) {
72 $this->cache = $cache;
73 }
74
82 public function key( string $keygroup, ...$components ) {
83 $this->key = $this->cache->makeKey( $keygroup, ...$components );
84
85 return $this;
86 }
87
95 public function globalKey( string $keygroup, ...$components ) {
96 $this->key = $this->cache->makeGlobalKey( $keygroup, ...$components );
97 return $this;
98 }
99
111 public function rawKey( string $key ) {
112 $this->key = $key;
113
114 return $this;
115 }
116
123 public function lifetime( int $seconds ) {
124 $this->lifetime = $seconds;
125 return $this;
126 }
127
133 public function keepIndefinitely() {
134 return $this->lifetime( WANObjectCache::TTL_INDEFINITE );
135 }
136
143 public function keepForAMinute() {
144 return $this->lifetime( WANObjectCache::TTL_MINUTE );
145 }
146
153 public function keepForAnHour() {
154 return $this->lifetime( WANObjectCache::TTL_HOUR );
155 }
156
163 public function keepForADay() {
164 return $this->lifetime( WANObjectCache::TTL_DAY );
165 }
166
173 public function keepForAWeek() {
174 return $this->lifetime( WANObjectCache::TTL_WEEK );
175 }
176
183 public function keepForAMonth() {
184 return $this->lifetime( WANObjectCache::TTL_MONTH );
185 }
186
193 public function keepForAYear() {
194 return $this->lifetime( WANObjectCache::TTL_YEAR );
195 }
196
211 public function invalidatedByKey( string $keygroup, ...$components ) {
212 $this->options['checkKeys'][] = $this->cache->makeKey( $keygroup, ...$components );
213
214 return $this;
215 }
216
225 public function invalidatedByGlobalKey( string $keygroup, ...$components ) {
226 $this->options['checkKeys'][] = $this->cache->makeGlobalKey( $keygroup, ...$components );
227
228 return $this;
229 }
230
242 public function lastModifiedCallback( callable $callback ) {
243 $this->options['touchedCallback'] = $callback;
244
245 return $this;
246 }
247
272 public function allowStale( int $seconds = 30 ) {
273 $this->options['lockTSE'] = $seconds;
274
275 return $this;
276 }
277
286 public function busyValue( $value ) {
287 $this->options['busyValue'] = $value;
288
289 return $this;
290 }
291
304 public function refreshBeforeExpiry( int $seconds ) {
305 $this->options['lowTTL'] = $seconds;
306
307 return $this;
308 }
309
322 public function refreshPopularEvery( int $seconds ) {
323 $this->options['hotTTR'] = $seconds;
324
325 return $this;
326 }
327
338 public function refreshPopularAfterAge( int $seconds ) {
339 $this->options['ageNew'] = $seconds;
340
341 return $this;
342 }
343
355 public function processCacheLifetime( int $seconds ) {
356 $this->options['pcTTL'] = $seconds;
357
358 return $this;
359 }
360
368 public function shortProcessCache() {
369 return $this->processCacheLifetime( WANObjectCache::TTL_PROC_SHORT );
370 }
371
379 public function longProcessCache() {
380 return $this->processCacheLifetime( WANObjectCache::TTL_PROC_LONG );
381 }
382
392 public function processCacheGroup( string $group ) {
393 $this->options['pcGroup'] = $group;
394
395 return $this;
396 }
397
409 public function valueVersion( int $version ) {
410 $this->options['version'] = $version;
411
412 return $this;
413 }
414
421 public function allowSegmentation( bool $enabled = true ) {
422 $this->options['segmentable'] = $enabled;
423
424 return $this;
425 }
426
433 public function callback( callable $callback ) {
434 $this->callback = $callback;
435
436 return $this;
437 }
438
444 public function fetch() {
445 if ( $this->key === null ) {
446 throw new LogicException( 'No cache key set; call key(), globalKey(), or rawKey() first' );
447 }
448 if ( $this->lifetime === null ) {
449 throw new LogicException( 'No lifetime set; call lifetime(), keepIndefinitely(), or e.g. keepForADay()' );
450 }
451 if ( $this->callback === null ) {
452 throw new LogicException( 'No callback set; call callback()' );
453 }
454
455 return $this->cache->getWithSetCallback(
456 $this->key,
457 $this->lifetime,
458 $this->callback,
459 $this->options
460 );
461 }
462}
Fluent builder for a WANObjectCache::getWithSetCallback() call.
lastModifiedCallback(callable $callback)
Treat the value as stale whenever a dynamic dependency has changed since it was generated.
shortProcessCache()
Keep the value in this PHP process for the life of a quick request.
busyValue( $value)
Return this placeholder when no value exists and another thread is generating one.
invalidatedByGlobalKey(string $keygroup,... $components)
Treat the value as stale whenever the given global "check" key is touched.
keepForAYear()
Reuse a newly generated value for one year.
refreshBeforeExpiry(int $seconds)
Consider regenerating early once the value has less than this long left to live.
processCacheGroup(string $group)
Use a dedicated process cache group instead of the primary one.
lifetime(int $seconds)
Set how long a newly generated value may be reused for.
keepForAMinute()
Reuse a newly generated value for one minute.
refreshPopularEvery(int $seconds)
Schedule an asynchronous refresh of popular keys about this often.
fetch()
Get the value at the key, generating and storing it via the callback if needed.
keepIndefinitely()
Reuse a newly generated value indefinitely, subject to LRU-style evictions.
longProcessCache()
Keep the value in this PHP process for long enough to survive a slow request.
refreshPopularAfterAge(int $seconds)
Only start refreshing a popular key once the value is this old.
processCacheLifetime(int $seconds)
Also keep the value in this PHP process for this many seconds.
keepForAMonth()
Reuse a newly generated value for one month.
rawKey(string $key)
Set the cache key from a key that was already built by makeKey() or makeGlobalKey()
keepForADay()
Reuse a newly generated value for one day.
allowStale(int $seconds=30)
Allow reuse of a recently expired value while another thread regenerates it.
valueVersion(int $version)
Set the format version of the value.
invalidatedByKey(string $keygroup,... $components)
Treat the value as stale whenever the given "check" key is touched.
keepForAWeek()
Reuse a newly generated value for one week.
keepForAnHour()
Reuse a newly generated value for one hour.
globalKey(string $keygroup,... $components)
Set the cache key, shared by all wikis of the farm.
allowSegmentation(bool $enabled=true)
Allow a large string value to be split over several cache entries.
key(string $keygroup,... $components)
Set the cache key, scoped to the current wiki.
Multi-datacenter aware caching interface.