MediaWiki REL1_34
CachedBagOStuff.php
Go to the documentation of this file.
1<?php
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}
if( $line===false) $args
Definition cdb.php:64
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:63
fieldHasFlags( $field, $flags)
Wrapper around a BagOStuff that caches data in memory.
__construct(BagOStuff $backend, $params=[])
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
getMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
makeGlobalKey( $class,... $components)
Make a global cache key.
unlock( $key)
Release an advisory lock on a key string.
deleteMulti(array $keys, $flags=0)
Batch deletion.
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
clearLastError()
Clear the "last error" registry.
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple keys that exist.
makeKeyInternal( $keyspace, $args)
Construct a cache key.
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
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.
HashBagOStuff $procCache
setMulti(array $data, $exptime=0, $flags=0)
Batch insertion/replace.
changeTTL( $key, $exptime=0, $flags=0)
Change the expiration on a key if it exists.
addBusyCallback(callable $workCallback)
Let a callback be run to avoid wasting time on special blocking calls.
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
Merge changes into the existing cache value (possibly creating a new one)
makeKey( $class,... $components)
Make a cache key, scoped to this instance's keyspace.
deleteObjectsExpiringBefore( $timestamp, callable $progress=null, $limit=INF)
Delete all objects expiring before a certain date.
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
Simple store for keeping values in an associative array for the current process.