29use Psr\Log\LoggerAwareInterface;
30use Psr\Log\LoggerInterface;
31use Psr\Log\NullLogger;
97 if ( isset(
$params[
'logger'] ) ) {
103 if ( isset(
$params[
'keyspace'] ) ) {
104 $this->keyspace =
$params[
'keyspace'];
107 $this->asyncHandler = isset(
$params[
'asyncHandler'] )
111 if ( !empty(
$params[
'reportDupes'] ) && is_callable( $this->asyncHandler ) ) {
112 $this->reportDupes =
true;
121 $this->logger = $logger;
128 $this->debugMode = $bool;
147 if ( !is_callable( $callback ) ) {
148 throw new InvalidArgumentException(
"Invalid cache miss callback provided." );
150 $value = call_user_func( $callback );
152 $this->
set( $key,
$value, $ttl );
173 public function get( $key,
$flags = 0, $oldFlags = null ) {
187 if ( !$this->reportDupes ) {
191 if ( !isset( $this->duplicateKeyLookups[$key] ) ) {
194 $this->duplicateKeyLookups[$key] = 0;
196 $this->duplicateKeyLookups[$key] += 1;
198 if ( $this->dupeTrackScheduled ===
false ) {
199 $this->dupeTrackScheduled =
true;
201 call_user_func( $this->asyncHandler,
function () {
202 $dups = array_filter( $this->duplicateKeyLookups );
203 foreach ( $dups
as $key =>
$count ) {
204 $this->logger->warning(
205 'Duplicate get(): "{key}" fetched {count} times',
207 [
'key' => $key,
'count' =>
$count + 1, ]
232 throw new Exception( __METHOD__ .
' not implemented.' );
244 abstract public function set( $key,
$value, $exptime = 0,
$flags = 0 );
252 abstract public function delete( $key );
268 public function merge( $key, $callback, $exptime = 0, $attempts = 10,
$flags = 0 ) {
269 if ( !is_callable( $callback ) ) {
270 throw new InvalidArgumentException(
"Got invalid callback." );
285 protected function mergeViaCas( $key, $callback, $exptime = 0, $attempts = 10 ) {
289 $currentValue = $this->
getWithToken( $key, $casToken, self::READ_LATEST );
295 $value = call_user_func( $callback, $this, $key, $currentValue );
300 } elseif ( $currentValue ===
false ) {
310 }
while ( !
$success && --$attempts );
325 protected function cas( $casToken, $key,
$value, $exptime = 0 ) {
326 throw new Exception(
"CAS is not implemented in " . __CLASS__ );
340 if ( !$this->
lock( $key, 6 ) ) {
345 $currentValue = $this->
get( $key, self::READ_LATEST );
350 $value = call_user_func( $callback, $this, $key, $currentValue );
358 if ( !$this->
unlock( $key ) ) {
360 trigger_error(
"Could not release lock for key '$key'." );
377 public function lock( $key, $timeout = 6, $expiry = 6, $rclass =
'' ) {
379 if ( isset( $this->locks[$key] ) ) {
380 if ( $rclass !=
'' && $this->locks[$key][
'class'] === $rclass ) {
381 ++$this->locks[$key][
'depth'];
388 $expiry = min( $expiry ?: INF, self::TTL_DAY );
392 if ( $this->
add(
"{$key}:lock", 1, $expiry ) ) {
398 $uRTT = max( 1e3, ceil( 1e6 * ( microtime(
true ) -
$timestamp ) ) );
403 if ( ++$attempts >= 3 &&
$sleep <= 5e5 ) {
409 $this->clearLastError();
410 $locked = $this->
add(
"{$key}:lock", 1, $expiry );
415 }
while ( !$locked && ( microtime(
true ) -
$timestamp ) < $timeout );
419 $this->locks[$key] = [
'class' => $rclass,
'depth' => 1 ];
432 if ( isset( $this->locks[$key] ) && --$this->locks[$key][
'depth'] <= 0 ) {
433 unset( $this->locks[$key] );
435 return $this->
delete(
"{$key}:lock" );
457 final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass =
'' ) {
458 $expiry = min( $expiry ?: INF, self::TTL_DAY );
460 if ( !$this->
lock( $key, $timeout, $expiry, $rclass ) ) {
464 $lSince = microtime(
true );
468 $age = ( microtime(
true ) - $lSince + $latency );
469 if ( ( $age + $latency ) >= $expiry ) {
470 $this->logger->warning(
"Lock for $key held too long ($age sec)." );
500 $val = $this->
get( $key );
501 if ( $val !==
false ) {
517 foreach ( $data
as $key =>
$value ) {
518 if ( !$this->
set( $key,
$value, $exptime ) ) {
532 if ( $this->
get( $key ) ===
false ) {
533 return $this->
set( $key,
$value, $exptime );
545 if ( !$this->
lock( $key ) ) {
548 $n = $this->
get( $key );
551 $this->
set( $key, max( 0, $n ) );
584 if ( $newValue ===
false ) {
586 $newValue = $this->
add( $key, (
int)$init, $ttl ) ? $init :
false;
588 if ( $newValue ===
false ) {
602 return $this->lastError;
610 $this->lastError = self::ERR_NONE;
619 $this->lastError = $err;
644 if ( $this->debugMode ) {
645 $this->logger->debug(
"{class} debug: $text", [
646 'class' => get_class( $this ),
657 if ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) ) {
658 return time() + $exptime;
672 if ( $exptime >= ( 10 * self::TTL_YEAR ) ) {
674 if ( $exptime <= 0 ) {
704 $arg = str_replace(
':',
'%3A', $arg );
705 $key = $key .
':' . $arg;
707 return strtr( $key,
' ',
'_' );
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
lock()
Lock the current instance of the parser.
interface is intended to be more or less compatible with the PHP memcached client.
getWithSetCallback( $key, $ttl, $callback, $flags=0)
Get an item with the given key, regenerating and setting it if not found.
__construct(array $params=[])
$params include:
getScopedLock( $key, $timeout=6, $expiry=30, $rclass='')
Get a lightweight exclusive self-unlocking lock.
unlock( $key)
Release an advisory lock on a key string.
incrWithInit( $key, $ttl, $value=1, $init=1)
Increase stored value of $key by $value while preserving its TTL.
isInteger( $value)
Check if a value is an integer.
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
array $duplicateKeyLookups
add( $key, $value, $exptime=0)
getMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
decr( $key, $value=1)
Decrease stored value of $key by $value while preserving its TTL.
modifySimpleRelayEvent(array $event)
Modify a cache update operation array for EventRelayer::notify()
const READ_LATEST
Bitfield constants for get()/getMulti()
deleteObjectsExpiringBefore( $date, $progressCallback=false)
Delete all objects expiring before a certain date.
trackDuplicateKeys( $key)
Track the number of times that a given key has been used.
convertExpiry( $exptime)
Convert an optionally relative time to an absolute time.
callback null $asyncHandler
getWithToken( $key, &$casToken, $flags=0)
merge( $key, $callback, $exptime=0, $attempts=10, $flags=0)
Merge changes into the existing cache value (possibly creating a new one).
setMulti(array $data, $exptime=0)
Batch insertion.
const ERR_NONE
Possible values for getLastError()
convertToRelative( $exptime)
Convert an optionally absolute expiry time to a relative time.
setLogger(LoggerInterface $logger)
makeGlobalKey()
Make a global cache key.
const WRITE_SYNC
Bitfield constants for set()/merge()
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
makeKey()
Make a cache key, scoped to this instance's keyspace.
setLastError( $err)
Set the "last error" registry.
mergeViaLock( $key, $callback, $exptime=0, $attempts=10, $flags=0)
clearLastError()
Clear the "last error" registry.
cas( $casToken, $key, $value, $exptime=0)
Check and set an item.
makeKeyInternal( $keyspace, $args)
Construct a cache key.
array[] $locks
Lock tracking.
mergeViaCas( $key, $callback, $exptime=0, $attempts=10)
incr( $key, $value=1)
Increase stored value of $key by $value while preserving its TTL.
Class for asserting that a callback happens when an dummy object leaves scope.
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
the array() calling protocol came about after MediaWiki 1.4rc1.
it s the revision text itself In either if gzip is the revision text is gzipped $flags
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
Generic base class for storage interfaces.