c97f56e474c5268f7a4b9585e65065df76555820
3 * Wrapper around a BagOStuff that caches data in memory
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.
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.
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
25 * Wrapper around a BagOStuff that caches data in memory
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).
36 class CachedBagOStuff
extends BagOStuff
{
39 /** @var HashBagOStuff */
43 * @param BagOStuff $backend Permanent backend to use
44 * @param array $params Parameters for HashBagOStuff
46 public function __construct( BagOStuff
$backend, $params = [] ) {
47 parent
::__construct( $params );
49 $this->backend
= $backend;
50 $this->procCache
= new HashBagOStuff( $params );
51 $this->attrMap
= $backend->attrMap
;
54 public function setDebug( $enabled ) {
55 parent
::setDebug( $enabled );
56 $this->backend
->setDebug( $enabled );
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
);
69 public function getMulti( array $keys, $flags = 0 ) {
70 $valuesByKeyCached = [];
73 foreach ( $keys as $key ) {
74 $value = $this->procCache
->get( $key, $flags );
75 if ( $value === false && !$this->procCache
->hasKey( $key ) ) {
76 $keysMissing[] = $key;
78 $valuesByKeyCached[$key] = $value;
82 $valuesByKeyFetched = $this->backend
->getMulti( $keys, $flags );
83 $this->setMulti( $valuesByKeyFetched, self
::TTL_INDEFINITE
, self
::WRITE_CACHE_ONLY
);
85 return $valuesByKeyCached +
$valuesByKeyFetched;
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 );
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 );
106 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
107 if ( $this->get( $key ) === false ) {
108 return $this->set( $key, $value, $exptime, $flags );
111 return false; // key already set
114 // These just call the backend (tested elsewhere)
115 // @codeCoverageIgnoreStart
117 public function merge( $key, callable
$callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
118 $this->procCache
->delete( $key );
120 return $this->backend
->merge( $key, $callback, $exptime, $attempts, $flags );
123 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
124 $this->procCache
->delete( $key );
126 return $this->backend
->changeTTL( $key, $exptime, $flags );
129 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
130 return $this->backend
->lock( $key, $timeout, $expiry, $rclass );
133 public function unlock( $key ) {
134 return $this->backend
->unlock( $key );
137 public function deleteObjectsExpiringBefore(
139 callable
$progress = null,
142 $this->procCache
->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
144 return $this->backend
->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
147 public function makeKeyInternal( $keyspace, $args ) {
148 return $this->backend
->makeKeyInternal( ...func_get_args() );
151 public function makeKey( $class, $component = null ) {
152 return $this->backend
->makeKey( ...func_get_args() );
155 public function makeGlobalKey( $class, $component = null ) {
156 return $this->backend
->makeGlobalKey( ...func_get_args() );
159 public function getLastError() {
160 return $this->backend
->getLastError();
163 public function clearLastError() {
164 return $this->backend
->clearLastError();
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 );
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 );
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 );
194 public function incr( $key, $value = 1 ) {
195 $this->procCache
->delete( $key );
197 return $this->backend
->incr( $key, $value );
200 public function decr( $key, $value = 1 ) {
201 $this->procCache
->delete( $key );
203 return $this->backend
->decr( $key, $value );
206 public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) {
207 $this->procCache
->delete( $key );
209 return $this->backend
->incrWithInit( $key, $ttl, $value, $init );
212 public function addBusyCallback( callable
$workCallback ) {
213 $this->backend
->addBusyCallback( $workCallback );
216 public function setMockTime( &$time ) {
217 parent
::setMockTime( $time );
218 $this->procCache
->setMockTime( $time );
219 $this->backend
->setMockTime( $time );
222 // @codeCoverageIgnoreEnd