MediaWiki  1.28.1
MapCacheLRU.php
Go to the documentation of this file.
1 <?php
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 
60  public function set( $key, $value ) {
61  if ( array_key_exists( $key, $this->cache ) ) {
62  $this->ping( $key );
63  } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
64  reset( $this->cache );
65  $evictKey = key( $this->cache );
66  unset( $this->cache[$evictKey] );
67  }
68  $this->cache[$key] = $value;
69  }
70 
77  public function has( $key ) {
78  return array_key_exists( $key, $this->cache );
79  }
80 
89  public function get( $key ) {
90  if ( !array_key_exists( $key, $this->cache ) ) {
91  return null;
92  }
93 
94  $this->ping( $key );
95 
96  return $this->cache[$key];
97  }
98 
103  public function getAllKeys() {
104  return array_keys( $this->cache );
105  }
106 
117  public function getWithSetCallback( $key, callable $callback ) {
118  if ( $this->has( $key ) ) {
119  $value = $this->get( $key );
120  } else {
121  $value = call_user_func( $callback );
122  if ( $value !== false ) {
123  $this->set( $key, $value );
124  }
125  }
126 
127  return $value;
128  }
129 
136  public function clear( $keys = null ) {
137  if ( $keys === null ) {
138  $this->cache = [];
139  } else {
140  foreach ( (array)$keys as $key ) {
141  unset( $this->cache[$key] );
142  }
143  }
144  }
145 
151  protected function ping( $key ) {
152  $item = $this->cache[$key];
153  unset( $this->cache[$key] );
154  $this->cache[$key] = $item;
155  }
156 }
ping($key)
Push an entry to the top of the cache.
the array() calling protocol came about after MediaWiki 1.4rc1.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
clear($keys=null)
Clear one or several cache entries, or all cache entries.
__construct($maxKeys)
Definition: MapCacheLRU.php:44
$value
set($key, $value)
Set a key/value pair.
Definition: MapCacheLRU.php:60
has($key)
Check if a key exists.
Definition: MapCacheLRU.php:77
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
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:25
array $cache
Definition: MapCacheLRU.php:36
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
getWithSetCallback($key, callable $callback)
Get an item with the given key, producing and setting it if not found.
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