]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/libs/objectcache/ReplicatedBagOStuff.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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
22 /**
23  * A cache class that directs writes to one set of servers and reads to
24  * another. This assumes that the servers used for reads are setup to replica DB
25  * those that writes go to. This can easily be used with redis for example.
26  *
27  * In the WAN scenario (e.g. multi-datacenter case), this is useful when
28  * writes are rare or they usually take place in the primary datacenter.
29  *
30  * @ingroup Cache
31  * @since 1.26
32  */
33 class ReplicatedBagOStuff extends BagOStuff {
34         /** @var BagOStuff */
35         protected $writeStore;
36         /** @var BagOStuff */
37         protected $readStore;
38
39         /**
40          * Constructor. Parameters are:
41          *   - writeFactory : ObjectFactory::getObjectFromSpec array yeilding BagOStuff.
42          *                    This object will be used for writes (e.g. the master DB).
43          *   - readFactory  : ObjectFactory::getObjectFromSpec array yeilding BagOStuff.
44          *                    This object will be used for reads (e.g. a replica DB).
45          *
46          * @param array $params
47          * @throws InvalidArgumentException
48          */
49         public function __construct( $params ) {
50                 parent::__construct( $params );
51
52                 if ( !isset( $params['writeFactory'] ) ) {
53                         throw new InvalidArgumentException(
54                                 __METHOD__ . ': the "writeFactory" parameter is required' );
55                 }
56                 if ( !isset( $params['readFactory'] ) ) {
57                         throw new InvalidArgumentException(
58                                 __METHOD__ . ': the "readFactory" parameter is required' );
59                 }
60
61                 $opts = [ 'reportDupes' => false ]; // redundant
62                 $this->writeStore = ( $params['writeFactory'] instanceof BagOStuff )
63                         ? $params['writeFactory']
64                         : ObjectFactory::getObjectFromSpec( $opts + $params['writeFactory'] );
65                 $this->readStore = ( $params['readFactory'] instanceof BagOStuff )
66                         ? $params['readFactory']
67                         : ObjectFactory::getObjectFromSpec( $opts + $params['readFactory'] );
68                 $this->attrMap = $this->mergeFlagMaps( [ $this->readStore, $this->writeStore ] );
69         }
70
71         public function setDebug( $debug ) {
72                 $this->writeStore->setDebug( $debug );
73                 $this->readStore->setDebug( $debug );
74         }
75
76         protected function doGet( $key, $flags = 0 ) {
77                 return ( $flags & self::READ_LATEST )
78                         ? $this->writeStore->get( $key, $flags )
79                         : $this->readStore->get( $key, $flags );
80         }
81
82         public function getMulti( array $keys, $flags = 0 ) {
83                 return ( $flags & self::READ_LATEST )
84                         ? $this->writeStore->getMulti( $keys, $flags )
85                         : $this->readStore->getMulti( $keys, $flags );
86         }
87
88         public function set( $key, $value, $exptime = 0, $flags = 0 ) {
89                 return $this->writeStore->set( $key, $value, $exptime, $flags );
90         }
91
92         public function delete( $key ) {
93                 return $this->writeStore->delete( $key );
94         }
95
96         public function add( $key, $value, $exptime = 0 ) {
97                 return $this->writeStore->add( $key, $value, $exptime );
98         }
99
100         public function incr( $key, $value = 1 ) {
101                 return $this->writeStore->incr( $key, $value );
102         }
103
104         public function decr( $key, $value = 1 ) {
105                 return $this->writeStore->decr( $key, $value );
106         }
107
108         public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
109                 return $this->writeStore->lock( $key, $timeout, $expiry, $rclass );
110         }
111
112         public function unlock( $key ) {
113                 return $this->writeStore->unlock( $key );
114         }
115
116         public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
117                 return $this->writeStore->merge( $key, $callback, $exptime, $attempts, $flags );
118         }
119
120         public function getLastError() {
121                 return ( $this->writeStore->getLastError() != self::ERR_NONE )
122                         ? $this->writeStore->getLastError()
123                         : $this->readStore->getLastError();
124         }
125
126         public function clearLastError() {
127                 $this->writeStore->clearLastError();
128                 $this->readStore->clearLastError();
129         }
130 }