MediaWiki  master
CachedBagOStuff.php
Go to the documentation of this file.
1 <?php
36 class CachedBagOStuff extends BagOStuff {
38  protected $backend;
40  protected $procCache;
41 
46  public function __construct( BagOStuff $backend, $params = [] ) {
47  parent::__construct( $params );
48 
49  $this->backend = $backend;
50  $this->procCache = new HashBagOStuff( $params );
51  $this->attrMap = $backend->attrMap;
52  }
53 
54  public function setDebug( $enabled ) {
55  parent::setDebug( $enabled );
56  $this->backend->setDebug( $enabled );
57  }
58 
59  public function get( $key, $flags = 0 ) {
60  $value = $this->procCache->get( $key, $flags );
61  if ( $value === false && !$this->procCache->hasKey( $key ) ) {
62  $value = $this->backend->get( $key, $flags );
63  $this->set( $key, $value, self::TTL_INDEFINITE, self::WRITE_CACHE_ONLY );
64  }
65 
66  return $value;
67  }
68 
69  public function getMulti( array $keys, $flags = 0 ) {
70  $valuesByKeyCached = [];
71 
72  $keysMissing = [];
73  foreach ( $keys as $key ) {
74  $value = $this->procCache->get( $key, $flags );
75  if ( $value === false && !$this->procCache->hasKey( $key ) ) {
76  $keysMissing[] = $key;
77  } else {
78  $valuesByKeyCached[$key] = $value;
79  }
80  }
81 
82  $valuesByKeyFetched = $this->backend->getMulti( $keysMissing, $flags );
83  $this->setMulti( $valuesByKeyFetched, self::TTL_INDEFINITE, self::WRITE_CACHE_ONLY );
84 
85  return $valuesByKeyCached + $valuesByKeyFetched;
86  }
87 
88  public function set( $key, $value, $exptime = 0, $flags = 0 ) {
89  $this->procCache->set( $key, $value, $exptime, $flags );
90 
91  if ( !$this->fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
92  $this->backend->set( $key, $value, $exptime, $flags );
93  }
94 
95  return true;
96  }
97 
98  public function delete( $key, $flags = 0 ) {
99  $this->procCache->delete( $key, $flags );
100 
101  if ( !$this->fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
102  $this->backend->delete( $key, $flags );
103  }
104 
105  return true;
106  }
107 
108  public function add( $key, $value, $exptime = 0, $flags = 0 ) {
109  if ( $this->get( $key ) === false ) {
110  return $this->set( $key, $value, $exptime, $flags );
111  }
112 
113  return false; // key already set
114  }
115 
116  // These just call the backend (tested elsewhere)
117  // @codeCoverageIgnoreStart
118 
119  public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
120  $this->procCache->delete( $key );
121 
122  return $this->backend->merge( $key, $callback, $exptime, $attempts, $flags );
123  }
124 
125  public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
126  $this->procCache->delete( $key );
127 
128  return $this->backend->changeTTL( $key, $exptime, $flags );
129  }
130 
131  public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
132  return $this->backend->lock( $key, $timeout, $expiry, $rclass );
133  }
134 
135  public function unlock( $key ) {
136  return $this->backend->unlock( $key );
137  }
138 
140  $timestamp,
141  callable $progress = null,
142  $limit = INF
143  ) {
144  $this->procCache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
145 
146  return $this->backend->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
147  }
148 
149  public function makeKeyInternal( $keyspace, $args ) {
150  return $this->backend->makeKeyInternal( $keyspace, $args );
151  }
152 
153  public function makeKey( $class, ...$components ) {
154  return $this->backend->makeKey( $class, ...$components );
155  }
156 
157  public function makeGlobalKey( $class, ...$components ) {
158  return $this->backend->makeGlobalKey( $class, ...$components );
159  }
160 
161  public function getLastError() {
162  return $this->backend->getLastError();
163  }
164 
165  public function clearLastError() {
166  return $this->backend->clearLastError();
167  }
168 
169  public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
170  $this->procCache->setMulti( $data, $exptime, $flags );
171 
172  if ( !$this->fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
173  return $this->backend->setMulti( $data, $exptime, $flags );
174  }
175 
176  return true;
177  }
178 
179  public function deleteMulti( array $keys, $flags = 0 ) {
180  $this->procCache->deleteMulti( $keys, $flags );
181 
182  if ( !$this->fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
183  return $this->backend->deleteMulti( $keys, $flags );
184  }
185 
186  return true;
187  }
188 
189  public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) {
190  $this->procCache->changeTTLMulti( $keys, $exptime, $flags );
191 
192  if ( !$this->fieldHasFlags( $flags, self::WRITE_CACHE_ONLY ) ) {
193  return $this->backend->changeTTLMulti( $keys, $exptime, $flags );
194  }
195 
196  return true;
197  }
198 
199  public function incr( $key, $value = 1, $flags = 0 ) {
200  $this->procCache->delete( $key );
201 
202  return $this->backend->incr( $key, $value, $flags );
203  }
204 
205  public function decr( $key, $value = 1, $flags = 0 ) {
206  $this->procCache->delete( $key );
207 
208  return $this->backend->decr( $key, $value, $flags );
209  }
210 
211  public function incrWithInit( $key, $exptime, $value = 1, $init = null, $flags = 0 ) {
212  $this->procCache->delete( $key );
213 
214  return $this->backend->incrWithInit( $key, $exptime, $value, $init, $flags );
215  }
216 
217  public function addBusyCallback( callable $workCallback ) {
218  $this->backend->addBusyCallback( $workCallback );
219  }
220 
221  public function setNewPreparedValues( array $valueByKey ) {
222  return $this->backend->setNewPreparedValues( $valueByKey );
223  }
224 
225  public function setMockTime( &$time ) {
226  parent::setMockTime( $time );
227  $this->procCache->setMockTime( $time );
228  $this->backend->setMockTime( $time );
229  }
230 
231  // @codeCoverageIgnoreEnd
232 }
CachedBagOStuff\incr
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
Definition: CachedBagOStuff.php:199
CachedBagOStuff\clearLastError
clearLastError()
Clear the "last error" registry.
Definition: CachedBagOStuff.php:165
CachedBagOStuff\setDebug
setDebug( $enabled)
Definition: CachedBagOStuff.php:54
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
CachedBagOStuff\unlock
unlock( $key)
Release an advisory lock on a key string.
Definition: CachedBagOStuff.php:135
CachedBagOStuff\getLastError
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
Definition: CachedBagOStuff.php:161
CachedBagOStuff\setNewPreparedValues
setNewPreparedValues(array $valueByKey)
Prepare values for storage and get their serialized sizes, or, estimate those sizes.
Definition: CachedBagOStuff.php:221
BagOStuff
Class representing a cache/ephemeral data store.
Definition: BagOStuff.php:70
CachedBagOStuff\__construct
__construct(BagOStuff $backend, $params=[])
Definition: CachedBagOStuff.php:46
CachedBagOStuff\decr
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
Definition: CachedBagOStuff.php:205
CachedBagOStuff\changeTTLMulti
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple keys that exist.
Definition: CachedBagOStuff.php:189
CachedBagOStuff\getMulti
getMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
Definition: CachedBagOStuff.php:69
CachedBagOStuff\addBusyCallback
addBusyCallback(callable $workCallback)
Let a callback be run to avoid wasting time on special blocking calls.
Definition: CachedBagOStuff.php:217
CachedBagOStuff\$backend
BagOStuff $backend
Definition: CachedBagOStuff.php:38
CachedBagOStuff\changeTTL
changeTTL( $key, $exptime=0, $flags=0)
Change the expiration on a key if it exists.
Definition: CachedBagOStuff.php:125
CachedBagOStuff\deleteObjectsExpiringBefore
deleteObjectsExpiringBefore( $timestamp, callable $progress=null, $limit=INF)
Delete all objects expiring before a certain date.
Definition: CachedBagOStuff.php:139
CachedBagOStuff\setMockTime
setMockTime(&$time)
Definition: CachedBagOStuff.php:225
CachedBagOStuff\makeKeyInternal
makeKeyInternal( $keyspace, $args)
Construct a cache key.
Definition: CachedBagOStuff.php:149
CachedBagOStuff\add
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
Definition: CachedBagOStuff.php:108
$args
if( $line===false) $args
Definition: mcc.php:124
CachedBagOStuff\incrWithInit
incrWithInit( $key, $exptime, $value=1, $init=null, $flags=0)
Increase the value of the given key (no TTL change) if it exists or create it otherwise.
Definition: CachedBagOStuff.php:211
CachedBagOStuff\deleteMulti
deleteMulti(array $keys, $flags=0)
Batch deletion.
Definition: CachedBagOStuff.php:179
CachedBagOStuff
Wrapper around a BagOStuff that caches data in memory.
Definition: CachedBagOStuff.php:36
CachedBagOStuff\$procCache
HashBagOStuff $procCache
Definition: CachedBagOStuff.php:40
CachedBagOStuff\makeKey
makeKey( $class,... $components)
Make a cache key, scoped to this instance's keyspace.
Definition: CachedBagOStuff.php:153
CachedBagOStuff\setMulti
setMulti(array $data, $exptime=0, $flags=0)
Batch insertion/replace.
Definition: CachedBagOStuff.php:169
CachedBagOStuff\merge
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
Merge changes into the existing cache value (possibly creating a new one)
Definition: CachedBagOStuff.php:119
BagOStuff\fieldHasFlags
fieldHasFlags( $field, $flags)
Definition: BagOStuff.php:508
$keys
$keys
Definition: testCompression.php:72
CachedBagOStuff\lock
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
Definition: CachedBagOStuff.php:131
CachedBagOStuff\makeGlobalKey
makeGlobalKey( $class,... $components)
Make a global cache key.
Definition: CachedBagOStuff.php:157
BagOStuff\WRITE_CACHE_ONLY
const WRITE_CACHE_ONLY
Definition: BagOStuff.php:90