Merge "Warn when user trying to block nonexistent user"
[lhc/web/wiklou.git] / includes / 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 * @author Aaron Schulz
21 */
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 slave
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 usually takes place on the primary datacenter.
30 *
31 * @ingroup Cache
32 * @since 1.25
33 */
34 class ReplicatedBagOStuff extends BagOStuff {
35 /** @var BagOStuff */
36 protected $mCache;
37 /** @var BagOStuff */
38 protected $sCache;
39
40 /**
41 * Constructor. Parameters are:
42 * - masterCache : Cache parameter structures, in the style required by $wgObjectCaches.
43 * See the documentation of $wgObjectCaches for more detail.
44 * - slaveCache : Cache parameter structures, in the style required by $wgObjectCaches.
45 * See the documentation of $wgObjectCaches for more detail.
46 *
47 * @param array $params
48 * @throws MWException
49 */
50 public function __construct( $params ) {
51 parent::__construct( $params );
52
53 if ( !isset( $params['masterCache'] ) ) {
54 throw new MWException( __METHOD__ . ': the "masterCache" parameter is required' );
55 } elseif ( !isset( $params['slaveCache'] ) ) {
56 throw new MWException( __METHOD__ . ': the "slaveCache" parameter is required' );
57 }
58
59 $this->mCache = ( $params['masterCache'] instanceof BagOStuff )
60 ? $params['masterCache']
61 : ObjectCache::newFromParams( $params['masterCache'] );
62 $this->sCache = ( $params['slaveCache'] instanceof BagOStuff )
63 ? $params['slaveCache']
64 : ObjectCache::newFromParams( $params['slaveCache'] );
65 }
66
67 public function setDebug( $debug ) {
68 $this->mCache->setDebug( $debug );
69 $this->sCache->setDebug( $debug );
70 }
71
72 public function get( $key, &$casToken = null ) {
73 return $this->sCache->get( $key, $casToken );
74 }
75
76 public function getMulti( $keys ) {
77 return $this->sCache->getMulti( $keys );
78 }
79
80 public function set( $key, $value, $exptime = 0 ) {
81 return $this->mCache->set( $key, $value, $exptime );
82 }
83
84 public function delete( $key ) {
85 return $this->mCache->delete( $key );
86 }
87
88 public function add( $key, $value, $exptime = 0 ) {
89 return $this->mCache->add( $key, $value, $exptime );
90 }
91
92 public function incr( $key, $value = 1 ) {
93 return $this->mCache->incr( $key, $value );
94 }
95
96 public function decr( $key ) {
97 return $this->mCache->decr( $key );
98 }
99
100 public function lock( $key, $timeout = 6, $expiry = 6 ) {
101 return $this->mCache->lock( $key, $timeout, $expiry );
102 }
103
104 public function unlock( $key ) {
105 return $this->mCache->unlock( $key );
106 }
107
108 public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
109 return $this->mCache->merge( $key, $callback, $exptime, $attempts );
110 }
111
112 public function getLastError() {
113 return ( $this->mCache->getLastError() != self::ERR_NONE )
114 ? $this->mCache->getLastError()
115 : $this->sCache->getLastError();
116 }
117
118 public function clearLastError() {
119 $this->mCache->clearLastError();
120 $this->sCache->clearLastError();
121 }
122 }