MediaWiki REL1_27
MultiWriteBagOStuff.php
Go to the documentation of this file.
1<?php
33 protected $caches;
35 protected $asyncWrites = false;
36
38 const ALL = INF;
39
40 const UPGRADE_TTL = 3600; // TTL when a key is copied to a higher cache tier
41
62 public function __construct( $params ) {
63 parent::__construct( $params );
64
65 if ( empty( $params['caches'] ) || !is_array( $params['caches'] ) ) {
66 throw new InvalidArgumentException(
67 __METHOD__ . ': "caches" parameter must be an array of caches'
68 );
69 }
70
71 $this->caches = [];
72 foreach ( $params['caches'] as $cacheInfo ) {
73 if ( $cacheInfo instanceof BagOStuff ) {
74 $this->caches[] = $cacheInfo;
75 } else {
76 if ( !isset( $cacheInfo['args'] ) ) {
77 // B/C for when $cacheInfo was for ObjectCache::newFromParams().
78 // Callers intenting this to be for ObjectFactory::getObjectFromSpec
79 // should have set "args" per the docs above. Doings so avoids extra
80 // (likely harmless) params (factory/class/calls) ending up in "args".
81 $cacheInfo['args'] = [ $cacheInfo ];
82 }
83 $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
84 }
85 }
86
87 $this->asyncWrites = (
88 isset( $params['replication'] ) &&
89 $params['replication'] === 'async' &&
90 is_callable( $this->asyncHandler )
91 );
92 }
93
94 public function setDebug( $debug ) {
95 foreach ( $this->caches as $cache ) {
96 $cache->setDebug( $debug );
97 }
98 }
99
100 protected function doGet( $key, $flags = 0 ) {
101 if ( ( $flags & self::READ_LATEST ) == self::READ_LATEST ) {
102 // If the latest write was a delete(), we do NOT want to fallback
103 // to the other tiers and possibly see the old value. Also, this
104 // is used by mergeViaLock(), which only needs to hit the primary.
105 return $this->caches[0]->get( $key, $flags );
106 }
107
108 $misses = 0; // number backends checked
109 $value = false;
110 foreach ( $this->caches as $cache ) {
111 $value = $cache->get( $key, $flags );
112 if ( $value !== false ) {
113 break;
114 }
115 ++$misses;
116 }
117
118 if ( $value !== false
119 && $misses > 0
120 && ( $flags & self::READ_VERIFIED ) == self::READ_VERIFIED
121 ) {
122 $this->doWrite( $misses, $this->asyncWrites, 'set', $key, $value, self::UPGRADE_TTL );
123 }
124
125 return $value;
126 }
127
128 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
129 $asyncWrites = ( ( $flags & self::WRITE_SYNC ) == self::WRITE_SYNC )
130 ? false
132
133 return $this->doWrite( self::ALL, $asyncWrites, 'set', $key, $value, $exptime );
134 }
135
136 public function delete( $key ) {
137 return $this->doWrite( self::ALL, $this->asyncWrites, 'delete', $key );
138 }
139
140 public function add( $key, $value, $exptime = 0 ) {
141 return $this->doWrite( self::ALL, $this->asyncWrites, 'add', $key, $value, $exptime );
142 }
143
144 public function incr( $key, $value = 1 ) {
145 return $this->doWrite( self::ALL, $this->asyncWrites, 'incr', $key, $value );
146 }
147
148 public function decr( $key, $value = 1 ) {
149 return $this->doWrite( self::ALL, $this->asyncWrites, 'decr', $key, $value );
150 }
151
152 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
153 // Only need to lock the first cache; also avoids deadlocks
154 return $this->caches[0]->lock( $key, $timeout, $expiry, $rclass );
155 }
156
157 public function unlock( $key ) {
158 // Only the first cache is locked
159 return $this->caches[0]->unlock( $key );
160 }
161
162 public function getLastError() {
163 return $this->caches[0]->getLastError();
164 }
165
166 public function clearLastError() {
167 $this->caches[0]->clearLastError();
168 }
169
179 protected function doWrite( $count, $asyncWrites, $method /*, ... */ ) {
180 $ret = true;
181 $args = array_slice( func_get_args(), 3 );
182
183 foreach ( $this->caches as $i => $cache ) {
184 if ( $i >= $count ) {
185 break; // ignore the lower tiers
186 }
187
188 if ( $i == 0 || !$asyncWrites ) {
189 // First store or in sync mode: write now and get result
190 if ( !call_user_func_array( [ $cache, $method ], $args ) ) {
191 $ret = false;
192 }
193 } else {
194 // Secondary write in async mode: do not block this HTTP request
196 call_user_func(
197 $this->asyncHandler,
198 function () use ( $cache, $method, $args, $logger ) {
199 if ( !call_user_func_array( [ $cache, $method ], $args ) ) {
200 $logger->warning( "Async $method op failed" );
201 }
202 }
203 );
204 }
205 }
206
207 return $ret;
208 }
209
218 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
219 $ret = false;
220 foreach ( $this->caches as $cache ) {
221 if ( $cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
222 $ret = true;
223 }
224 }
225
226 return $ret;
227 }
228}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$i
Definition Parser.php:1694
if( $line===false) $args
Definition cdb.php:64
interface is intended to be more or less compatible with the PHP memcached client.
Definition BagOStuff.php:45
const WRITE_SYNC
Bitfield constants for set()/merge()
Definition BagOStuff.php:83
LoggerInterface $logger
Definition BagOStuff.php:56
A cache class that replicates all writes to multiple child caches.
doWrite( $count, $asyncWrites, $method)
Apply a write method to the first $count backing caches.
add( $key, $value, $exptime=0)
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
__construct( $params)
$params include:
clearLastError()
Clear the "last error" registry.
incr( $key, $value=1)
Increase stored value of $key by $value while preserving its TTL.
bool $asyncWrites
Use async secondary writes.
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
const ALL
Idiom for "write to all backends".
deleteObjectsExpiringBefore( $date, $progressCallback=false)
Delete objects expiring before a certain date.
decr( $key, $value=1)
Decrease stored value of $key by $value while preserving its TTL.
unlock( $key)
Release an advisory lock on a key string.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:1810
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition hooks.txt:2555
processing should stop and the error should be shown to the user * false
Definition hooks.txt:189
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
$debug
Definition mcc.php:31
$cache
Definition mcc.php:33
$params