MediaWiki
REL1_23
|
00001 <?php 00028 class ProcessCacheLRU { 00030 protected $cache = array(); // (key => prop => value) 00032 protected $cacheTimes = array(); // (key => prop => UNIX timestamp) 00033 00034 protected $maxCacheKeys; // integer; max entries 00035 00040 public function __construct( $maxKeys ) { 00041 if ( !is_int( $maxKeys ) || $maxKeys < 1 ) { 00042 throw new UnexpectedValueException( __METHOD__ . " must be given an integer >= 1" ); 00043 } 00044 $this->maxCacheKeys = $maxKeys; 00045 } 00046 00057 public function set( $key, $prop, $value ) { 00058 if ( isset( $this->cache[$key] ) ) { 00059 $this->ping( $key ); // push to top 00060 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) { 00061 reset( $this->cache ); 00062 $evictKey = key( $this->cache ); 00063 unset( $this->cache[$evictKey] ); 00064 unset( $this->cacheTimes[$evictKey] ); 00065 } 00066 $this->cache[$key][$prop] = $value; 00067 $this->cacheTimes[$key][$prop] = time(); 00068 } 00069 00078 public function has( $key, $prop, $maxAge = 0 ) { 00079 if ( isset( $this->cache[$key][$prop] ) ) { 00080 return ( $maxAge <= 0 || ( time() - $this->cacheTimes[$key][$prop] ) <= $maxAge ); 00081 } 00082 00083 return false; 00084 } 00085 00095 public function get( $key, $prop ) { 00096 if ( isset( $this->cache[$key][$prop] ) ) { 00097 $this->ping( $key ); // push to top 00098 return $this->cache[$key][$prop]; 00099 } else { 00100 return null; 00101 } 00102 } 00103 00110 public function clear( $keys = null ) { 00111 if ( $keys === null ) { 00112 $this->cache = array(); 00113 $this->cacheTimes = array(); 00114 } else { 00115 foreach ( (array)$keys as $key ) { 00116 unset( $this->cache[$key] ); 00117 unset( $this->cacheTimes[$key] ); 00118 } 00119 } 00120 } 00121 00127 protected function ping( $key ) { 00128 $item = $this->cache[$key]; 00129 unset( $this->cache[$key] ); 00130 $this->cache[$key] = $item; 00131 } 00132 }