MediaWiki  REL1_31
MapCacheLRU.php
Go to the documentation of this file.
1 <?php
23 use Wikimedia\Assert\Assert;
24 
34 class MapCacheLRU {
36  protected $cache = []; // (key => value)
37 
38  protected $maxCacheKeys; // integer; max entries
39 
44  public function __construct( $maxKeys ) {
45  Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
46  Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
47 
48  $this->maxCacheKeys = $maxKeys;
49  }
50 
57  public static function newFromArray( array $values, $maxKeys ) {
58  $mapCache = new self( $maxKeys );
59  $mapCache->cache = ( count( $values ) > $maxKeys )
60  ? array_slice( $values, -$maxKeys, null, true )
61  : $values;
62 
63  return $mapCache;
64  }
65 
70  public function toArray() {
71  return $this->cache;
72  }
73 
89  public function set( $key, $value, $rank = 1.0 ) {
90  if ( $this->has( $key ) ) {
91  $this->ping( $key );
92  } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
93  reset( $this->cache );
94  $evictKey = key( $this->cache );
95  unset( $this->cache[$evictKey] );
96  }
97 
98  if ( $rank < 1.0 && $rank > 0 ) {
99  $offset = intval( $rank * count( $this->cache ) );
100  $this->cache = array_slice( $this->cache, 0, $offset, true )
101  + [ $key => $value ]
102  + array_slice( $this->cache, $offset, null, true );
103  } else {
104  $this->cache[$key] = $value;
105  }
106  }
107 
114  public function has( $key ) {
115  if ( !is_int( $key ) && !is_string( $key ) ) {
116  throw new UnexpectedValueException(
117  __METHOD__ . ' called with invalid key. Must be string or integer.' );
118  }
119  return array_key_exists( $key, $this->cache );
120  }
121 
130  public function get( $key ) {
131  if ( !$this->has( $key ) ) {
132  return null;
133  }
134 
135  $this->ping( $key );
136 
137  return $this->cache[$key];
138  }
139 
144  public function getAllKeys() {
145  return array_keys( $this->cache );
146  }
147 
159  public function getWithSetCallback( $key, callable $callback, $rank = 1.0 ) {
160  if ( $this->has( $key ) ) {
161  $value = $this->get( $key );
162  } else {
163  $value = call_user_func( $callback );
164  if ( $value !== false ) {
165  $this->set( $key, $value, $rank );
166  }
167  }
168 
169  return $value;
170  }
171 
178  public function clear( $keys = null ) {
179  if ( $keys === null ) {
180  $this->cache = [];
181  } else {
182  foreach ( (array)$keys as $key ) {
183  unset( $this->cache[$key] );
184  }
185  }
186  }
187 
193  protected function ping( $key ) {
194  $item = $this->cache[$key];
195  unset( $this->cache[$key] );
196  $this->cache[$key] = $item;
197  }
198 }
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
MapCacheLRU\ping
ping( $key)
Push an entry to the top of the cache.
Definition: MapCacheLRU.php:193
array
the array() calling protocol came about after MediaWiki 1.4rc1.
MapCacheLRU\getAllKeys
getAllKeys()
Definition: MapCacheLRU.php:144
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:55
MapCacheLRU\$cache
array $cache
Definition: MapCacheLRU.php:36
php
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
key
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling but I prefer the flexibility This should also do the output encoding The system allocates a global one in $wgOut Title Represents the title of an and does all the work of translating among various forms such as plain database key
Definition: design.txt:26
MapCacheLRU\__construct
__construct( $maxKeys)
Definition: MapCacheLRU.php:44
MapCacheLRU\getWithSetCallback
getWithSetCallback( $key, callable $callback, $rank=1.0)
Get an item with the given key, producing and setting it if not found.
Definition: MapCacheLRU.php:159
MapCacheLRU\$maxCacheKeys
$maxCacheKeys
Definition: MapCacheLRU.php:38
MapCacheLRU\newFromArray
static newFromArray(array $values, $maxKeys)
Definition: MapCacheLRU.php:57
MapCacheLRU
Handles a simple LRU key/value map with a maximum number of entries.
Definition: MapCacheLRU.php:34
$value
$value
Definition: styleTest.css.php:45
MapCacheLRU\has
has( $key)
Check if a key exists.
Definition: MapCacheLRU.php:114
as
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:22
$keys
$keys
Definition: testCompression.php:67
MapCacheLRU\clear
clear( $keys=null)
Clear one or several cache entries, or all cache entries.
Definition: MapCacheLRU.php:178
MapCacheLRU\toArray
toArray()
Definition: MapCacheLRU.php:70