MediaWiki REL1_30
MapCacheLRU.php
Go to the documentation of this file.
1<?php
23use Wikimedia\Assert\Assert;
24
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 ( $this->has( $key ) ) {
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 if ( !is_int( $key ) && !is_string( $key ) ) {
79 throw new MWException( __METHOD__ . ' called with invalid key. Must be string or integer.' );
80 }
81 return array_key_exists( $key, $this->cache );
82 }
83
92 public function get( $key ) {
93 if ( !$this->has( $key ) ) {
94 return null;
95 }
96
97 $this->ping( $key );
98
99 return $this->cache[$key];
100 }
101
106 public function getAllKeys() {
107 return array_keys( $this->cache );
108 }
109
120 public function getWithSetCallback( $key, callable $callback ) {
121 if ( $this->has( $key ) ) {
122 $value = $this->get( $key );
123 } else {
124 $value = call_user_func( $callback );
125 if ( $value !== false ) {
126 $this->set( $key, $value );
127 }
128 }
129
130 return $value;
131 }
132
139 public function clear( $keys = null ) {
140 if ( $keys === null ) {
141 $this->cache = [];
142 } else {
143 foreach ( (array)$keys as $key ) {
144 unset( $this->cache[$key] );
145 }
146 }
147 }
148
154 protected function ping( $key ) {
155 $item = $this->cache[$key];
156 unset( $this->cache[$key] );
157 $this->cache[$key] = $item;
158 }
159}
MediaWiki exception.
Handles a simple LRU key/value map with a maximum number of entries.
has( $key)
Check if a key exists.
__construct( $maxKeys)
ping( $key)
Push an entry to the top of the cache.
getWithSetCallback( $key, callable $callback)
Get an item with the given key, producing and setting it if not found.
clear( $keys=null)
Clear one or several cache entries, or all cache entries.
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
you have access to all of the normal MediaWiki so you can get a DB use the cache
$cache
Definition mcc.php:33