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
20 * @author Aaron Schulz
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.
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 parameters yeilding BagOStuff.
43 * This object will be used for writes (e.g. the master DB).
44 * - readFactory : ObjectFactory::getObjectFromSpec parameters yeilding BagOStuff.
45 * This object will be used for reads (e.g. a slave 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' );
56 } elseif ( !isset( $params['readFactory'] ) ) {
57 throw new InvalidArgumentException(
58 __METHOD__
. ': the "readFactory" parameter is required' );
61 $this->writeStore
= ( $params['writeFactory'] instanceof BagOStuff
)
62 ?
$params['writeFactory']
63 : ObjectFactory
::getObjectFromSpec( $params['writeFactory'] );
64 $this->readStore
= ( $params['readFactory'] instanceof BagOStuff
)
65 ?
$params['readFactory']
66 : ObjectFactory
::getObjectFromSpec( $params['readFactory'] );
69 public function setDebug( $debug ) {
70 $this->writeStore
->setDebug( $debug );
71 $this->readStore
->setDebug( $debug );
74 public function get( $key, &$casToken = null ) {
75 return $this->readStore
->get( $key, $casToken );
78 public function getMulti( $keys ) {
79 return $this->readStore
->getMulti( $keys );
82 public function set( $key, $value, $exptime = 0 ) {
83 return $this->writeStore
->set( $key, $value, $exptime );
86 public function delete( $key ) {
87 return $this->writeStore
->delete( $key );
90 public function add( $key, $value, $exptime = 0 ) {
91 return $this->writeStore
->add( $key, $value, $exptime );
94 public function incr( $key, $value = 1 ) {
95 return $this->writeStore
->incr( $key, $value );
98 public function decr( $key ) {
99 return $this->writeStore
->decr( $key );
102 public function lock( $key, $timeout = 6, $expiry = 6 ) {
103 return $this->writeStore
->lock( $key, $timeout, $expiry );
106 public function unlock( $key ) {
107 return $this->writeStore
->unlock( $key );
110 public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
111 return $this->writeStore
->merge( $key, $callback, $exptime, $attempts );
114 public function getLastError() {
115 return ( $this->writeStore
->getLastError() != self
::ERR_NONE
)
116 ?
$this->writeStore
->getLastError()
117 : $this->readStore
->getLastError();
120 public function clearLastError() {
121 $this->writeStore
->clearLastError();
122 $this->readStore
->clearLastError();