[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
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 a simple LRU key/value map with a maximum number of entries 26 * 27 * Use ProcessCacheLRU if hierarchical purging is needed or objects can become stale 28 * 29 * @see ProcessCacheLRU 30 * @ingroup Cache 31 * @since 1.23 32 */ 33 class MapCacheLRU { 34 /** @var array */ 35 protected $cache = array(); // (key => value) 36 37 protected $maxCacheKeys; // integer; max entries 38 39 /** 40 * @param int $maxKeys Maximum number of entries allowed (min 1). 41 * @throws MWException When $maxCacheKeys is not an int or =< 0. 42 */ 43 public function __construct( $maxKeys ) { 44 if ( !is_int( $maxKeys ) || $maxKeys < 1 ) { 45 throw new MWException( __METHOD__ . " must be given an integer and >= 1" ); 46 } 47 $this->maxCacheKeys = $maxKeys; 48 } 49 50 /** 51 * Set a key/value pair. 52 * This will prune the cache if it gets too large based on LRU. 53 * If the item is already set, it will be pushed to the top of the cache. 54 * 55 * @param string $key 56 * @param mixed $value 57 * @return void 58 */ 59 public function set( $key, $value ) { 60 if ( array_key_exists( $key, $this->cache ) ) { 61 $this->ping( $key ); // push to top 62 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) { 63 reset( $this->cache ); 64 $evictKey = key( $this->cache ); 65 unset( $this->cache[$evictKey] ); 66 } 67 $this->cache[$key] = $value; 68 } 69 70 /** 71 * Check if a key exists 72 * 73 * @param string $key 74 * @return bool 75 */ 76 public function has( $key ) { 77 return array_key_exists( $key, $this->cache ); 78 } 79 80 /** 81 * Get the value for a key. 82 * This returns null if the key is not set. 83 * If the item is already set, it will be pushed to the top of the cache. 84 * 85 * @param string $key 86 * @return mixed 87 */ 88 public function get( $key ) { 89 if ( array_key_exists( $key, $this->cache ) ) { 90 $this->ping( $key ); // push to top 91 return $this->cache[$key]; 92 } else { 93 return null; 94 } 95 } 96 97 /** 98 * Clear one or several cache entries, or all cache entries 99 * 100 * @param string|array $keys 101 * @return void 102 */ 103 public function clear( $keys = null ) { 104 if ( $keys === null ) { 105 $this->cache = array(); 106 } else { 107 foreach ( (array)$keys as $key ) { 108 unset( $this->cache[$key] ); 109 } 110 } 111 } 112 113 /** 114 * Push an entry to the top of the cache 115 * 116 * @param string $key 117 */ 118 protected function ping( $key ) { 119 $item = $this->cache[$key]; 120 unset( $this->cache[$key] ); 121 $this->cache[$key] = $item; 122 } 123 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |