Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / libs / objectcache / CachedBagOStuff.php
1 <?php
2 /**
3 * Wrapper around a BagOStuff that caches data in memory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Wrapper around a BagOStuff that caches data in memory
26 *
27 * The differences between CachedBagOStuff and MultiWriteBagOStuff are:
28 * * CachedBagOStuff supports only one "backend".
29 * * There's a flag for writes to only go to the in-memory cache.
30 * * The in-memory cache is always updated.
31 * * Locks go to the backend cache (with MultiWriteBagOStuff, it would wind
32 * up going to the HashBagOStuff used for the in-memory cache).
33 *
34 * @ingroup Cache
35 */
36 class CachedBagOStuff extends BagOStuff {
37 /** @var BagOStuff */
38 protected $backend;
39 /** @var HashBagOStuff */
40 protected $procCache;
41
42 /**
43 * @param BagOStuff $backend Permanent backend to use
44 * @param array $params Parameters for HashBagOStuff
45 */
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( $keys, $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 if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
91 $this->backend->set( $key, $value, $exptime, $flags );
92 }
93
94 return true;
95 }
96
97 public function delete( $key, $flags = 0 ) {
98 $this->procCache->delete( $key, $flags );
99 if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
100 $this->backend->delete( $key, $flags );
101 }
102
103 return true;
104 }
105
106 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
107 if ( $this->get( $key ) === false ) {
108 return $this->set( $key, $value, $exptime, $flags );
109 }
110
111 return false; // key already set
112 }
113
114 // These just call the backend (tested elsewhere)
115 // @codeCoverageIgnoreStart
116
117 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
118 $this->procCache->delete( $key );
119
120 return $this->backend->merge( $key, $callback, $exptime, $attempts, $flags );
121 }
122
123 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
124 $this->procCache->delete( $key );
125
126 return $this->backend->changeTTL( $key, $exptime, $flags );
127 }
128
129 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
130 return $this->backend->lock( $key, $timeout, $expiry, $rclass );
131 }
132
133 public function unlock( $key ) {
134 return $this->backend->unlock( $key );
135 }
136
137 public function deleteObjectsExpiringBefore(
138 $timestamp,
139 callable $progress = null,
140 $limit = INF
141 ) {
142 $this->procCache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
143
144 return $this->backend->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
145 }
146
147 public function makeKeyInternal( $keyspace, $args ) {
148 return $this->backend->makeKeyInternal( $keyspace, $args );
149 }
150
151 public function makeKey( $class, ...$components ) {
152 return $this->backend->makeKey( $class, ...$components );
153 }
154
155 public function makeGlobalKey( $class, ...$components ) {
156 return $this->backend->makeGlobalKey( $class, ...$components );
157 }
158
159 public function getLastError() {
160 return $this->backend->getLastError();
161 }
162
163 public function clearLastError() {
164 return $this->backend->clearLastError();
165 }
166
167 public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
168 $this->procCache->setMulti( $data, $exptime, $flags );
169 if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
170 return $this->backend->setMulti( $data, $exptime, $flags );
171 }
172
173 return true;
174 }
175
176 public function deleteMulti( array $keys, $flags = 0 ) {
177 $this->procCache->deleteMulti( $keys, $flags );
178 if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
179 return $this->backend->deleteMulti( $keys, $flags );
180 }
181
182 return true;
183 }
184
185 public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) {
186 $this->procCache->changeTTLMulti( $keys, $exptime, $flags );
187 if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
188 return $this->backend->changeTTLMulti( $keys, $exptime, $flags );
189 }
190
191 return true;
192 }
193
194 public function incr( $key, $value = 1 ) {
195 $this->procCache->delete( $key );
196
197 return $this->backend->incr( $key, $value );
198 }
199
200 public function decr( $key, $value = 1 ) {
201 $this->procCache->delete( $key );
202
203 return $this->backend->decr( $key, $value );
204 }
205
206 public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) {
207 $this->procCache->delete( $key );
208
209 return $this->backend->incrWithInit( $key, $ttl, $value, $init );
210 }
211
212 public function addBusyCallback( callable $workCallback ) {
213 $this->backend->addBusyCallback( $workCallback );
214 }
215
216 public function setMockTime( &$time ) {
217 parent::setMockTime( $time );
218 $this->procCache->setMockTime( $time );
219 $this->backend->setMockTime( $time );
220 }
221
222 // @codeCoverageIgnoreEnd
223 }