Merge "Avoid showing crazy staleness times at ActiveUsers"
[lhc/web/wiklou.git] / includes / libs / ProcessCacheLRU.php
1 <?php
2 /**
3 * Per-process memory cache for storing items.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Cache
22 */
23
24 /**
25 * Handles per process caching of items
26 * @ingroup Cache
27 */
28 class ProcessCacheLRU {
29 /** @var Array */
30 protected $cache = array(); // (key => prop => value)
31 /** @var Array */
32 protected $cacheTimes = array(); // (key => prop => UNIX timestamp)
33
34 protected $maxCacheKeys; // integer; max entries
35
36 /**
37 * @param $maxKeys integer Maximum number of entries allowed (min 1).
38 * @throws UnexpectedValueException When $maxCacheKeys is not an int or =< 0.
39 */
40 public function __construct( $maxKeys ) {
41 if ( !is_int( $maxKeys ) || $maxKeys < 1 ) {
42 throw new UnexpectedValueException( __METHOD__ . " must be given an integer >= 1" );
43 }
44 $this->maxCacheKeys = $maxKeys;
45 }
46
47 /**
48 * Set a property field for a cache entry.
49 * This will prune the cache if it gets too large based on LRU.
50 * If the item is already set, it will be pushed to the top of the cache.
51 *
52 * @param $key string
53 * @param $prop string
54 * @param $value mixed
55 * @return void
56 */
57 public function set( $key, $prop, $value ) {
58 if ( isset( $this->cache[$key] ) ) {
59 $this->ping( $key ); // push to top
60 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
61 reset( $this->cache );
62 $evictKey = key( $this->cache );
63 unset( $this->cache[$evictKey] );
64 unset( $this->cacheTimes[$evictKey] );
65 }
66 $this->cache[$key][$prop] = $value;
67 $this->cacheTimes[$key][$prop] = time();
68 }
69
70 /**
71 * Check if a property field exists for a cache entry.
72 *
73 * @param $key string
74 * @param $prop string
75 * @param $maxAge integer Ignore items older than this many seconds (since 1.21)
76 * @return bool
77 */
78 public function has( $key, $prop, $maxAge = 0 ) {
79 if ( isset( $this->cache[$key][$prop] ) ) {
80 return ( $maxAge <= 0 || ( time() - $this->cacheTimes[$key][$prop] ) <= $maxAge );
81 }
82
83 return false;
84 }
85
86 /**
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.
90 *
91 * @param $key string
92 * @param $prop string
93 * @return mixed
94 */
95 public function get( $key, $prop ) {
96 if ( isset( $this->cache[$key][$prop] ) ) {
97 $this->ping( $key ); // push to top
98 return $this->cache[$key][$prop];
99 } else {
100 return null;
101 }
102 }
103
104 /**
105 * Clear one or several cache entries, or all cache entries
106 *
107 * @param $keys string|Array
108 * @return void
109 */
110 public function clear( $keys = null ) {
111 if ( $keys === null ) {
112 $this->cache = array();
113 $this->cacheTimes = array();
114 } else {
115 foreach ( (array)$keys as $key ) {
116 unset( $this->cache[$key] );
117 unset( $this->cacheTimes[$key] );
118 }
119 }
120 }
121
122 /**
123 * Push an entry to the top of the cache
124 *
125 * @param $key string
126 */
127 protected function ping( $key ) {
128 $item = $this->cache[$key];
129 unset( $this->cache[$key] );
130 $this->cache[$key] = $item;
131 }
132 }