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 setMockTime( &$time ) {
222  parent::setMockTime( $time );
223  $this->procCache->setMockTime( $time );
224  $this->backend->setMockTime( $time );
225  }
226 
227  // @codeCoverageIgnoreEnd
228 }
changeTTL( $key, $exptime=0, $flags=0)
makeGlobalKey( $class,... $components)
Make a global cache key.
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
deleteMulti(array $keys, $flags=0)
makeKeyInternal( $keyspace, $args)
incrWithInit( $key, $exptime, $value=1, $init=null, $flags=0)
if( $line===false) $args
Definition: cdb.php:64
add( $key, $value, $exptime=0, $flags=0)
deleteObjectsExpiringBefore( $timestamp, callable $progress=null, $limit=INF)
HashBagOStuff $procCache
makeKey( $class,... $components)
Make a cache key, scoped to this instance&#39;s keyspace.
setMulti(array $data, $exptime=0, $flags=0)
changeTTLMulti(array $keys, $exptime, $flags=0)
getMulti(array $keys, $flags=0)
setDebug( $enabled)
decr( $key, $value=1, $flags=0)
incr( $key, $value=1, $flags=0)
addBusyCallback(callable $workCallback)
lock( $key, $timeout=6, $expiry=6, $rclass='')
__construct(BagOStuff $backend, $params=[])
fieldHasFlags( $field, $flags)
Definition: BagOStuff.php:493