Merge "Title: Title::getSubpage should not lose the interwiki prefix"
[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 unset( $params['reportDupes'] ); // useless here
48
49 parent::__construct( $params );
50
51 $this->backend = $backend;
52 $this->procCache = new HashBagOStuff( $params );
53 $this->attrMap = $backend->attrMap;
54 }
55
56 protected function doGet( $key, $flags = 0, &$casToken = null ) {
57 $ret = $this->procCache->get( $key, $flags );
58 if ( $ret === false && !$this->procCache->hasKey( $key ) ) {
59 $ret = $this->backend->get( $key, $flags );
60 $this->set( $key, $ret, self::TTL_INDEFINITE, self::WRITE_CACHE_ONLY );
61 }
62
63 return $ret;
64 }
65
66 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
67 $this->procCache->set( $key, $value, $exptime, $flags );
68 if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
69 $this->backend->set( $key, $value, $exptime, $flags );
70 }
71
72 return true;
73 }
74
75 protected function doDelete( $key, $flags = 0 ) {
76 $this->procCache->delete( $key, $flags );
77 if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
78 $this->backend->delete( $key, $flags );
79 }
80
81 return true;
82 }
83
84 public function deleteObjectsExpiringBefore(
85 $timestamp,
86 callable $progress = null,
87 $limit = INF
88 ) {
89 $this->procCache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
90
91 return $this->backend->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
92 }
93
94 // These just call the backend (tested elsewhere)
95 // @codeCoverageIgnoreStart
96
97 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
98 if ( $this->get( $key ) === false ) {
99 return $this->set( $key, $value, $exptime, $flags );
100 }
101
102 return false; // key already set
103 }
104
105 public function incr( $key, $value = 1 ) {
106 $n = $this->backend->incr( $key, $value );
107
108 $this->procCache->delete( $key );
109
110 return $n;
111 }
112
113 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
114 return $this->backend->lock( $key, $timeout, $expiry, $rclass );
115 }
116
117 public function unlock( $key ) {
118 return $this->backend->unlock( $key );
119 }
120
121 public function makeKeyInternal( $keyspace, $args ) {
122 return $this->backend->makeKeyInternal( ...func_get_args() );
123 }
124
125 public function makeKey( $class, $component = null ) {
126 return $this->backend->makeKey( ...func_get_args() );
127 }
128
129 public function makeGlobalKey( $class, $component = null ) {
130 return $this->backend->makeGlobalKey( ...func_get_args() );
131 }
132
133 public function setDebug( $bool ) {
134 parent::setDebug( $bool );
135 $this->backend->setDebug( $bool );
136 }
137
138 public function getLastError() {
139 return $this->backend->getLastError();
140 }
141
142 public function clearLastError() {
143 return $this->backend->clearLastError();
144 }
145
146 // @codeCoverageIgnoreEnd
147 }