Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[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( $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
139 public function deleteObjectsExpiringBefore(
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 }