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