MediaWiki REL1_31
ProcessCacheLRU.php
Go to the documentation of this file.
1<?php
23use Wikimedia\Assert\Assert;
24
34 protected $cache = []; // (key => prop => value)
35
37 protected $cacheTimes = []; // (key => prop => UNIX timestamp)
38
39 protected $maxCacheKeys; // integer; max entries
40
45 public function __construct( $maxKeys ) {
46 $this->resize( $maxKeys );
47 }
48
59 public function set( $key, $prop, $value ) {
60 if ( isset( $this->cache[$key] ) ) {
61 $this->ping( $key );
62 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
63 reset( $this->cache );
64 $evictKey = key( $this->cache );
65 unset( $this->cache[$evictKey] );
66 unset( $this->cacheTimes[$evictKey] );
67 }
68 $this->cache[$key][$prop] = $value;
69 $this->cacheTimes[$key][$prop] = microtime( true );
70 }
71
80 public function has( $key, $prop, $maxAge = 0.0 ) {
81 if ( isset( $this->cache[$key][$prop] ) ) {
82 return ( $maxAge <= 0 ||
83 ( microtime( true ) - $this->cacheTimes[$key][$prop] ) <= $maxAge
84 );
85 }
86
87 return false;
88 }
89
99 public function get( $key, $prop ) {
100 if ( !isset( $this->cache[$key][$prop] ) ) {
101 return null;
102 }
103 $this->ping( $key );
104 return $this->cache[$key][$prop];
105 }
106
113 public function clear( $keys = null ) {
114 if ( $keys === null ) {
115 $this->cache = [];
116 $this->cacheTimes = [];
117 } else {
118 foreach ( (array)$keys as $key ) {
119 unset( $this->cache[$key] );
120 unset( $this->cacheTimes[$key] );
121 }
122 }
123 }
124
132 public function resize( $maxKeys ) {
133 Assert::parameterType( 'integer', $maxKeys, '$maxKeys' );
134 Assert::parameter( $maxKeys > 0, '$maxKeys', 'must be above zero' );
135
136 $this->maxCacheKeys = $maxKeys;
137 while ( count( $this->cache ) > $this->maxCacheKeys ) {
138 reset( $this->cache );
139 $evictKey = key( $this->cache );
140 unset( $this->cache[$evictKey] );
141 unset( $this->cacheTimes[$evictKey] );
142 }
143 }
144
150 protected function ping( $key ) {
151 $item = $this->cache[$key];
152 unset( $this->cache[$key] );
153 $this->cache[$key] = $item;
154 }
155
160 public function getSize() {
161 return $this->maxCacheKeys;
162 }
163}
Class for process caching individual properties of expiring 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