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.
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.
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
21 use Wikimedia\ObjectFactory
;
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.
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.
34 class ReplicatedBagOStuff
extends BagOStuff
{
36 protected $writeStore;
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).
47 * @param array $params
48 * @throws InvalidArgumentException
50 public function __construct( $params ) {
51 parent
::__construct( $params );
53 if ( !isset( $params['writeFactory'] ) ) {
54 throw new InvalidArgumentException(
55 __METHOD__
. ': the "writeFactory" parameter is required' );
57 if ( !isset( $params['readFactory'] ) ) {
58 throw new InvalidArgumentException(
59 __METHOD__
. ': the "readFactory" parameter is required' );
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
] );
72 public function setDebug( $enabled ) {
73 parent
::setDebug( $enabled );
74 $this->writeStore
->setDebug( $enabled );
75 $this->readStore
->setDebug( $enabled );
78 public function get( $key, $flags = 0 ) {
79 return $this->fieldHasFlags( $flags, self
::READ_LATEST
)
80 ?
$this->writeStore
->get( $key, $flags )
81 : $this->readStore
->get( $key, $flags );
84 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
85 return $this->writeStore
->set( $key, $value, $exptime, $flags );
88 public function delete( $key, $flags = 0 ) {
89 return $this->writeStore
->delete( $key, $flags );
92 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
93 return $this->writeStore
->add( $key, $value, $exptime, $flags );
96 public function merge( $key, callable
$callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
97 return $this->writeStore
->merge( $key, $callback, $exptime, $attempts, $flags );
100 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
101 return $this->writeStore
->changeTTL( $key, $exptime, $flags );
104 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
105 return $this->writeStore
->lock( $key, $timeout, $expiry, $rclass );
108 public function unlock( $key ) {
109 return $this->writeStore
->unlock( $key );
112 public function deleteObjectsExpiringBefore(
114 callable
$progress = null,
117 return $this->writeStore
->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
120 public function getMulti( array $keys, $flags = 0 ) {
121 return $this->fieldHasFlags( $flags, self
::READ_LATEST
)
122 ?
$this->writeStore
->getMulti( $keys, $flags )
123 : $this->readStore
->getMulti( $keys, $flags );
126 public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
127 return $this->writeStore
->setMulti( $data, $exptime, $flags );
130 public function deleteMulti( array $keys, $flags = 0 ) {
131 return $this->writeStore
->deleteMulti( $keys, $flags );
134 public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) {
135 return $this->writeStore
->changeTTLMulti( $keys, $exptime, $flags );
138 public function incr( $key, $value = 1, $flags = 0 ) {
139 return $this->writeStore
->incr( $key, $value, $flags );
142 public function decr( $key, $value = 1, $flags = 0 ) {
143 return $this->writeStore
->decr( $key, $value, $flags );
146 public function incrWithInit( $key, $exptime, $value = 1, $init = null, $flags = 0 ) {
147 return $this->writeStore
->incrWithInit( $key, $exptime, $value, $init, $flags );
150 public function getLastError() {
151 return ( $this->writeStore
->getLastError() != self
::ERR_NONE
)
152 ?
$this->writeStore
->getLastError()
153 : $this->readStore
->getLastError();
156 public function clearLastError() {
157 $this->writeStore
->clearLastError();
158 $this->readStore
->clearLastError();
161 public function makeKeyInternal( $keyspace, $args ) {
162 return $this->writeStore
->makeKeyInternal( ...func_get_args() );
165 public function makeKey( $class, ...$components ) {
166 return $this->writeStore
->makeKey( ...func_get_args() );
169 public function makeGlobalKey( $class, ...$components ) {
170 return $this->writeStore
->makeGlobalKey( ...func_get_args() );
173 public function addBusyCallback( callable
$workCallback ) {
174 $this->writeStore
->addBusyCallback( $workCallback );
177 public function setMockTime( &$time ) {
178 parent
::setMockTime( $time );
179 $this->writeStore
->setMockTime( $time );
180 $this->readStore
->setMockTime( $time );