Title: Title::getSubpage should not lose the interwiki prefix
[lhc/web/wiklou.git] / includes / libs / objectcache / ReplicatedBagOStuff.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Cache
20 */
21 use Wikimedia\ObjectFactory;
22
23 /**
24 * A cache class that directs writes to one set of servers and reads to
25 * another. This assumes that the servers used for reads are setup to replica DB
26 * those that writes go to. This can easily be used with redis for example.
27 *
28 * In the WAN scenario (e.g. multi-datacenter case), this is useful when
29 * writes are rare or they usually take place in the primary datacenter.
30 *
31 * @ingroup Cache
32 * @since 1.26
33 */
34 class ReplicatedBagOStuff extends BagOStuff {
35 /** @var BagOStuff */
36 protected $writeStore;
37 /** @var BagOStuff */
38 protected $readStore;
39
40 /**
41 * Constructor. Parameters are:
42 * - writeFactory : ObjectFactory::getObjectFromSpec array yeilding BagOStuff.
43 * This object will be used for writes (e.g. the master DB).
44 * - readFactory : ObjectFactory::getObjectFromSpec array yeilding BagOStuff.
45 * This object will be used for reads (e.g. a replica DB).
46 *
47 * @param array $params
48 * @throws InvalidArgumentException
49 */
50 public function __construct( $params ) {
51 parent::__construct( $params );
52
53 if ( !isset( $params['writeFactory'] ) ) {
54 throw new InvalidArgumentException(
55 __METHOD__ . ': the "writeFactory" parameter is required' );
56 }
57 if ( !isset( $params['readFactory'] ) ) {
58 throw new InvalidArgumentException(
59 __METHOD__ . ': the "readFactory" parameter is required' );
60 }
61
62 $opts = [ 'reportDupes' => false ]; // redundant
63 $this->writeStore = ( $params['writeFactory'] instanceof BagOStuff )
64 ? $params['writeFactory']
65 : ObjectFactory::getObjectFromSpec( $opts + $params['writeFactory'] );
66 $this->readStore = ( $params['readFactory'] instanceof BagOStuff )
67 ? $params['readFactory']
68 : ObjectFactory::getObjectFromSpec( $opts + $params['readFactory'] );
69 $this->attrMap = $this->mergeFlagMaps( [ $this->readStore, $this->writeStore ] );
70 }
71
72 public function setDebug( $debug ) {
73 $this->writeStore->setDebug( $debug );
74 $this->readStore->setDebug( $debug );
75 }
76
77 public function get( $key, $flags = 0 ) {
78 return ( ( $flags & self::READ_LATEST ) == self::READ_LATEST )
79 ? $this->writeStore->get( $key, $flags )
80 : $this->readStore->get( $key, $flags );
81 }
82
83 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
84 return $this->writeStore->set( $key, $value, $exptime, $flags );
85 }
86
87 public function delete( $key, $flags = 0 ) {
88 return $this->writeStore->delete( $key, $flags );
89 }
90
91 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
92 return $this->writeStore->add( $key, $value, $exptime, $flags );
93 }
94
95 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
96 return $this->writeStore->merge( $key, $callback, $exptime, $attempts, $flags );
97 }
98
99 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
100 return $this->writeStore->changeTTL( $key, $exptime, $flags );
101 }
102
103 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
104 return $this->writeStore->lock( $key, $timeout, $expiry, $rclass );
105 }
106
107 public function unlock( $key ) {
108 return $this->writeStore->unlock( $key );
109 }
110
111 public function deleteObjectsExpiringBefore(
112 $timestamp,
113 callable $progressCallback = null,
114 $limit = INF
115 ) {
116 return $this->writeStore->deleteObjectsExpiringBefore(
117 $timestamp,
118 $progressCallback,
119 $limit
120 );
121 }
122
123 public function getMulti( array $keys, $flags = 0 ) {
124 return ( ( $flags & self::READ_LATEST ) == self::READ_LATEST )
125 ? $this->writeStore->getMulti( $keys, $flags )
126 : $this->readStore->getMulti( $keys, $flags );
127 }
128
129 public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
130 return $this->writeStore->setMulti( $data, $exptime, $flags );
131 }
132
133 public function deleteMulti( array $keys, $flags = 0 ) {
134 return $this->writeStore->deleteMulti( $keys, $flags );
135 }
136
137 public function incr( $key, $value = 1 ) {
138 return $this->writeStore->incr( $key, $value );
139 }
140
141 public function decr( $key, $value = 1 ) {
142 return $this->writeStore->decr( $key, $value );
143 }
144
145 public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) {
146 return $this->writeStore->incrWithInit( $key, $ttl, $value, $init );
147 }
148
149 public function getLastError() {
150 return ( $this->writeStore->getLastError() != self::ERR_NONE )
151 ? $this->writeStore->getLastError()
152 : $this->readStore->getLastError();
153 }
154
155 public function clearLastError() {
156 $this->writeStore->clearLastError();
157 $this->readStore->clearLastError();
158 }
159
160 public function makeKeyInternal( $keyspace, $args ) {
161 return $this->writeStore->makeKeyInternal( ...func_get_args() );
162 }
163
164 public function makeKey( $class, $component = null ) {
165 return $this->writeStore->makeKey( ...func_get_args() );
166 }
167
168 public function makeGlobalKey( $class, $component = null ) {
169 return $this->writeStore->makeGlobalKey( ...func_get_args() );
170 }
171
172 protected function doGet( $key, $flags = 0, &$casToken = null ) {
173 throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
174 }
175
176 protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
177 throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
178 }
179
180 protected function doDelete( $key, $flags = 0 ) {
181 throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
182 }
183
184 protected function doGetMulti( array $keys, $flags = 0 ) {
185 throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
186 }
187
188 protected function serialize( $value ) {
189 throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
190 }
191
192 protected function unserialize( $blob ) {
193 throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
194 }
195 }