Merge "prop=imageinfo&iiprop=url|thumbmime needs iiurlwidth="
[lhc/web/wiklou.git] / includes / cache / 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
32 protected $maxCacheKeys; // integer; max entries
33
34 /**
35 * @param $maxKeys integer Maximum number of entries allowed (min 1).
36 * @throws MWException When $maxCacheKeys is not an int or =< 0.
37 */
38 public function __construct( $maxKeys ) {
39 if ( !is_int( $maxKeys ) || $maxKeys < 1 ) {
40 throw new MWException( __METHOD__ . " must be given an integer and >= 1" );
41 }
42 $this->maxCacheKeys = $maxKeys;
43 }
44
45 /**
46 * Set a property field for a cache entry.
47 * This will prune the cache if it gets too large.
48 * If the item is already set, it will be pushed to the top of the cache.
49 *
50 * @param $key string
51 * @param $prop string
52 * @param $value mixed
53 * @return void
54 */
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 unset( $this->cache[key( $this->cache )] );
61 }
62 $this->cache[$key][$prop] = $value;
63 }
64
65 /**
66 * Check if a property field exists for a cache entry.
67 *
68 * @param $key string
69 * @param $prop string
70 * @return bool
71 */
72 public function has( $key, $prop ) {
73 return isset( $this->cache[$key][$prop] );
74 }
75
76 /**
77 * Get a property field for a cache entry.
78 * This returns null if the property is not set.
79 * If the item is already set, it will be pushed to the top of the cache.
80 *
81 * @param $key string
82 * @param $prop string
83 * @return mixed
84 */
85 public function get( $key, $prop ) {
86 if ( isset( $this->cache[$key][$prop] ) ) {
87 $this->ping( $key ); // push to top
88 return $this->cache[$key][$prop];
89 } else {
90 return null;
91 }
92 }
93
94 /**
95 * Clear one or several cache entries, or all cache entries
96 *
97 * @param $keys string|Array
98 * @return void
99 */
100 public function clear( $keys = null ) {
101 if ( $keys === null ) {
102 $this->cache = array();
103 } else {
104 foreach ( (array)$keys as $key ) {
105 unset( $this->cache[$key] );
106 }
107 }
108 }
109
110 /**
111 * Push an entry to the top of the cache
112 *
113 * @param $key string
114 */
115 protected function ping( $key ) {
116 $item = $this->cache[$key];
117 unset( $this->cache[$key] );
118 $this->cache[$key] = $item;
119 }
120 }