MediaWiki REL1_30
ProcessCacheLRU.php
Go to the documentation of this file.
1<?php
23use Wikimedia\Assert\Assert;
24
31 protected $cache = []; // (key => prop => value)
32
34 protected $cacheTimes = []; // (key => prop => UNIX timestamp)
35
36 protected $maxCacheKeys; // integer; max entries
37
42 public function __construct( $maxKeys ) {
43 $this->resize( $maxKeys );
44 }
45
56 public function set( $key, $prop, $value ) {
57 if ( isset( $this->cache[$key] ) ) {
58 $this->ping( $key );
59 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
60 reset( $this->cache );
61 $evictKey = key( $this->cache );
62 unset( $this->cache[$evictKey] );
63 unset( $this->cacheTimes[$evictKey] );
64 }
65 $this->cache[$key][$prop] = $value;
66 $this->cacheTimes[$key][$prop] = microtime( true );
67 }
68
77 public function has( $key, $prop, $maxAge = 0.0 ) {
78 if ( isset( $this->cache[$key][$prop] ) ) {
79 return ( $maxAge <= 0 ||
80 ( microtime( true ) - $this->cacheTimes[$key][$prop] ) <= $maxAge
81 );
82 }
83
84 return false;
85 }
86
96 public function get( $key, $prop ) {
97 if ( !isset( $this->cache[$key][$prop] ) ) {
98 return null;
99 }
100 $this->ping( $key );
101 return $this->cache[$key][$prop];
102 }
103
110 public function clear( $keys = null ) {
111 if ( $keys === null ) {
112 $this->cache = [];
113 $this->cacheTimes = [];
114 } else {
115 foreach ( (array)$keys as $key ) {
116 unset( $this->cache[$key] );
117 unset( $this->cacheTimes[$key] );
118 }
119 }
120 }
121
129 public function resize( $maxKeys ) {
130 Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
131 Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
132
133 $this->maxCacheKeys = $maxKeys;
134 while ( count( $this->cache ) > $this->maxCacheKeys ) {
135 reset( $this->cache );
136 $evictKey = key( $this->cache );
137 unset( $this->cache[$evictKey] );
138 unset( $this->cacheTimes[$evictKey] );
139 }
140 }
141
147 protected function ping( $key ) {
148 $item = $this->cache[$key];
149 unset( $this->cache[$key] );
150 $this->cache[$key] = $item;
151 }
152
157 public function getSize() {
158 return $this->maxCacheKeys;
159 }
160}
Handles per process caching of items.
clear( $keys=null)
Clear one or several cache entries, or all cache entries.
has( $key, $prop, $maxAge=0.0)
Check if a property field exists for a cache entry.
__construct( $maxKeys)
ping( $key)
Push an entry to the top of the cache.
resize( $maxKeys)
Resize the maximum number of cache entries, removing older entries as needed.
getSize()
Get cache size.
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