3 * Per-process memory cache for storing items.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Handles per process caching of items
28 class ProcessCacheLRU
{
30 protected $cache = array(); // (key => prop => value)
33 protected $cacheTimes = array(); // (key => prop => UNIX timestamp)
35 protected $maxCacheKeys; // integer; max entries
38 * @param int $maxKeys Maximum number of entries allowed (min 1).
39 * @throws UnexpectedValueException When $maxCacheKeys is not an int or =< 0.
41 public function __construct( $maxKeys ) {
42 $this->resize( $maxKeys );
46 * Set a property field for a cache entry.
47 * This will prune the cache if it gets too large based on LRU.
48 * If the item is already set, it will be pushed to the top of the cache.
55 public function set( $key, $prop, $value ) {
56 if ( isset( $this->cache
[$key] ) ) {
57 $this->ping( $key ); // push to top
58 } elseif ( count( $this->cache
) >= $this->maxCacheKeys
) {
59 reset( $this->cache
);
60 $evictKey = key( $this->cache
);
61 unset( $this->cache
[$evictKey] );
62 unset( $this->cacheTimes
[$evictKey] );
64 $this->cache
[$key][$prop] = $value;
65 $this->cacheTimes
[$key][$prop] = microtime( true );
69 * Check if a property field exists for a cache entry.
73 * @param float $maxAge Ignore items older than this many seconds (since 1.21)
76 public function has( $key, $prop, $maxAge = 0.0 ) {
77 if ( isset( $this->cache
[$key][$prop] ) ) {
78 return ( $maxAge <= 0 ||
79 ( microtime( true ) - $this->cacheTimes
[$key][$prop] ) <= $maxAge
87 * Get a property field for a cache entry.
88 * This returns null if the property is not set.
89 * If the item is already set, it will be pushed to the top of the cache.
95 public function get( $key, $prop ) {
96 if ( isset( $this->cache
[$key][$prop] ) ) {
99 return $this->cache
[$key][$prop];
106 * Clear one or several cache entries, or all cache entries.
108 * @param string|array $keys
111 public function clear( $keys = null ) {
112 if ( $keys === null ) {
113 $this->cache
= array();
114 $this->cacheTimes
= array();
116 foreach ( (array)$keys as $key ) {
117 unset( $this->cache
[$key] );
118 unset( $this->cacheTimes
[$key] );
124 * Resize the maximum number of cache entries, removing older entries as needed
126 * @param int $maxKeys
128 * @throws UnexpectedValueException
130 public function resize( $maxKeys ) {
131 if ( !is_int( $maxKeys ) ||
$maxKeys < 1 ) {
132 throw new UnexpectedValueException( __METHOD__
. " must be given an integer >= 1" );
134 $this->maxCacheKeys
= $maxKeys;
135 while ( count( $this->cache
) > $this->maxCacheKeys
) {
136 reset( $this->cache
);
137 $evictKey = key( $this->cache
);
138 unset( $this->cache
[$evictKey] );
139 unset( $this->cacheTimes
[$evictKey] );
144 * Push an entry to the top of the cache
148 protected function ping( $key ) {
149 $item = $this->cache
[$key];
150 unset( $this->cache
[$key] );
151 $this->cache
[$key] = $item;