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 * @TODO: Make this class use composition instead of calling super
36 */
37 class CachedBagOStuff extends HashBagOStuff {
38 /** @var BagOStuff */
39 protected $backend;
40
41 /**
42 * @param BagOStuff $backend Permanent backend to use
43 * @param array $params Parameters for HashBagOStuff
44 */
45 function __construct( BagOStuff $backend, $params = [] ) {
46 unset( $params['reportDupes'] ); // useless here
47
48 parent::__construct( $params );
49
50 $this->backend = $backend;
51 $this->attrMap = $backend->attrMap;
52 }
53
54 public function get( $key, $flags = 0 ) {
55 $ret = parent::get( $key, $flags );
56 if ( $ret === false && !$this->hasKey( $key ) ) {
57 $ret = $this->backend->get( $key, $flags );
58 $this->set( $key, $ret, 0, self::WRITE_CACHE_ONLY );
59 }
60 return $ret;
61 }
62
63 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
64 parent::set( $key, $value, $exptime, $flags );
65 if ( !( $flags & self::WRITE_CACHE_ONLY ) ) {
66 $this->backend->set( $key, $value, $exptime, $flags & ~self::WRITE_CACHE_ONLY );
67 }
68 return true;
69 }
70
71 public function delete( $key, $flags = 0 ) {
72 parent::delete( $key, $flags );
73 if ( !( $flags & self::WRITE_CACHE_ONLY ) ) {
74 $this->backend->delete( $key );
75 }
76
77 return true;
78 }
79
80 public function setDebug( $bool ) {
81 parent::setDebug( $bool );
82 $this->backend->setDebug( $bool );
83 }
84
85 public function deleteObjectsExpiringBefore(
86 $timestamp,
87 callable $progressCallback = null,
88 $limit = INF
89 ) {
90 parent::deleteObjectsExpiringBefore( $timestamp, $progressCallback, $limit );
91
92 return $this->backend->deleteObjectsExpiringBefore(
93 $timestamp,
94 $progressCallback,
95 $limit
96 );
97 }
98
99 public function makeKeyInternal( $keyspace, $args ) {
100 return $this->backend->makeKeyInternal( ...func_get_args() );
101 }
102
103 public function makeKey( $class, $component = null ) {
104 return $this->backend->makeKey( ...func_get_args() );
105 }
106
107 public function makeGlobalKey( $class, $component = null ) {
108 return $this->backend->makeGlobalKey( ...func_get_args() );
109 }
110
111 // These just call the backend (tested elsewhere)
112 // @codeCoverageIgnoreStart
113
114 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
115 if ( $this->get( $key ) === false ) {
116 return $this->set( $key, $value, $exptime, $flags );
117 }
118
119 return false; // key already set
120 }
121
122 public function incr( $key, $value = 1 ) {
123 $n = $this->backend->incr( $key, $value );
124 parent::delete( $key );
125
126 return $n;
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 getLastError() {
138 return $this->backend->getLastError();
139 }
140
141 public function clearLastError() {
142 return $this->backend->clearLastError();
143 }
144
145 // @codeCoverageIgnoreEnd
146 }