MediaWiki  master
CachedBagOStuff.php
Go to the documentation of this file.
1 <?php
36 class CachedBagOStuff extends BagOStuff {
38  protected $backend;
40  protected $procCache;
41 
46  public function __construct( BagOStuff $backend, $params = [] ) {
47  parent::__construct( $params );
48 
49  $this->backend = $backend;
50  $this->procCache = new HashBagOStuff( $params );
51  $this->attrMap = $backend->attrMap;
52  }
53 
54  public function setDebug( $enabled ) {
55  parent::setDebug( $enabled );
56  $this->backend->setDebug( $enabled );
57  }
58 
59  public function get( $key, $flags = 0 ) {
60  $value = $this->procCache->get( $key, $flags );
61  if ( $value === false && !$this->procCache->hasKey( $key ) ) {
62  $value = $this->backend->get( $key, $flags );
63  $this->set( $key, $value, self::TTL_INDEFINITE, self::WRITE_CACHE_ONLY );
64  }
65 
66  return $value;
67  }
68 
69  public function getMulti( array $keys, $flags = 0 ) {
70  $valuesByKeyCached = [];
71 
72  $keysMissing = [];
73  foreach ( $keys as $key ) {
74  $value = $this->procCache->get( $key, $flags );
75  if ( $value === false && !$this->procCache->hasKey( $key ) ) {
76  $keysMissing[] = $key;
77  } else {
78  $valuesByKeyCached[$key] = $value;
79  }
80  }
81 
82  $valuesByKeyFetched = $this->backend->getMulti( $keysMissing, $flags );
83  $this->setMulti( $valuesByKeyFetched, self::TTL_INDEFINITE, self::WRITE_CACHE_ONLY );
84 
85  return $valuesByKeyCached + $valuesByKeyFetched;
86  }
87 
88  public function set( $key, $value, $exptime = 0, $flags = 0 ) {
89  $this->procCache->set( $key, $value, $exptime, $flags );
90  if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
91  $this->backend->set( $key, $value, $exptime, $flags );
92  }
93 
94  return true;
95  }
96 
97  public function delete( $key, $flags = 0 ) {
98  $this->procCache->delete( $key, $flags );
99  if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
100  $this->backend->delete( $key, $flags );
101  }
102 
103  return true;
104  }
105 
106  public function add( $key, $value, $exptime = 0, $flags = 0 ) {
107  if ( $this->get( $key ) === false ) {
108  return $this->set( $key, $value, $exptime, $flags );
109  }
110 
111  return false; // key already set
112  }
113 
114  // These just call the backend (tested elsewhere)
115  // @codeCoverageIgnoreStart
116 
117  public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
118  $this->procCache->delete( $key );
119 
120  return $this->backend->merge( $key, $callback, $exptime, $attempts, $flags );
121  }
122 
123  public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
124  $this->procCache->delete( $key );
125 
126  return $this->backend->changeTTL( $key, $exptime, $flags );
127  }
128 
129  public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
130  return $this->backend->lock( $key, $timeout, $expiry, $rclass );
131  }
132 
133  public function unlock( $key ) {
134  return $this->backend->unlock( $key );
135  }
136 
138  $timestamp,
139  callable $progress = null,
140  $limit = INF
141  ) {
142  $this->procCache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
143 
144  return $this->backend->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
145  }
146 
147  public function makeKeyInternal( $keyspace, $args ) {
148  return $this->backend->makeKeyInternal( $keyspace, $args );
149  }
150 
151  public function makeKey( $class, ...$components ) {
152  return $this->backend->makeKey( $class, ...$components );
153  }
154 
155  public function makeGlobalKey( $class, ...$components ) {
156  return $this->backend->makeGlobalKey( $class, ...$components );
157  }
158 
159  public function getLastError() {
160  return $this->backend->getLastError();
161  }
162 
163  public function clearLastError() {
164  return $this->backend->clearLastError();
165  }
166 
167  public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
168  $this->procCache->setMulti( $data, $exptime, $flags );
169  if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
170  return $this->backend->setMulti( $data, $exptime, $flags );
171  }
172 
173  return true;
174  }
175 
176  public function deleteMulti( array $keys, $flags = 0 ) {
177  $this->procCache->deleteMulti( $keys, $flags );
178  if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
179  return $this->backend->deleteMulti( $keys, $flags );
180  }
181 
182  return true;
183  }
184 
185  public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) {
186  $this->procCache->changeTTLMulti( $keys, $exptime, $flags );
187  if ( ( $flags & self::WRITE_CACHE_ONLY ) != self::WRITE_CACHE_ONLY ) {
188  return $this->backend->changeTTLMulti( $keys, $exptime, $flags );
189  }
190 
191  return true;
192  }
193 
194  public function incr( $key, $value = 1 ) {
195  $this->procCache->delete( $key );
196 
197  return $this->backend->incr( $key, $value );
198  }
199 
200  public function decr( $key, $value = 1 ) {
201  $this->procCache->delete( $key );
202 
203  return $this->backend->decr( $key, $value );
204  }
205 
206  public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) {
207  $this->procCache->delete( $key );
208 
209  return $this->backend->incrWithInit( $key, $ttl, $value, $init );
210  }
211 
212  public function addBusyCallback( callable $workCallback ) {
213  $this->backend->addBusyCallback( $workCallback );
214  }
215 
216  public function setMockTime( &$time ) {
217  parent::setMockTime( $time );
218  $this->procCache->setMockTime( $time );
219  $this->backend->setMockTime( $time );
220  }
221 
222  // @codeCoverageIgnoreEnd
223 }
changeTTL( $key, $exptime=0, $flags=0)
makeGlobalKey( $class,... $components)
Make a global cache key.
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
decr( $key, $value=1)
deleteMulti(array $keys, $flags=0)
$value
makeKeyInternal( $keyspace, $args)
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition: hooks.txt:1787
if( $line===false) $args
Definition: cdb.php:64
add( $key, $value, $exptime=0, $flags=0)
deleteObjectsExpiringBefore( $timestamp, callable $progress=null, $limit=INF)
incr( $key, $value=1)
$params
HashBagOStuff $procCache
makeKey( $class,... $components)
Make a cache key, scoped to this instance&#39;s keyspace.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:773
setMulti(array $data, $exptime=0, $flags=0)
changeTTLMulti(array $keys, $exptime, $flags=0)
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
Definition: distributors.txt:9
getMulti(array $keys, $flags=0)
setDebug( $enabled)
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:35
incrWithInit( $key, $ttl, $value=1, $init=1)
addBusyCallback(callable $workCallback)
lock( $key, $timeout=6, $expiry=6, $rclass='')
__construct(BagOStuff $backend, $params=[])