MediaWiki  REL1_20
ProcessCacheLRU.php
Go to the documentation of this file.
00001 <?php
00028 class ProcessCacheLRU {
00030         protected $cache = array(); // (key => prop => value)
00031 
00032         protected $maxCacheKeys; // integer; max entries
00033 
00038         public function __construct( $maxKeys ) {
00039                 if ( !is_int( $maxKeys ) || $maxKeys < 1 ) {
00040                         throw new MWException( __METHOD__ . " must be given an integer and >= 1" );
00041                 }
00042                 $this->maxCacheKeys = $maxKeys;
00043         }
00044 
00055         public function set( $key, $prop, $value ) {
00056                 if ( isset( $this->cache[$key] ) ) {
00057                         $this->ping( $key ); // push to top
00058                 } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
00059                         reset( $this->cache );
00060                         unset( $this->cache[key( $this->cache )] );
00061                 }
00062                 $this->cache[$key][$prop] = $value;
00063         }
00064 
00072         public function has( $key, $prop ) {
00073                 return isset( $this->cache[$key][$prop] );
00074         }
00075 
00085         public function get( $key, $prop ) {
00086                 if ( isset( $this->cache[$key][$prop] ) ) {
00087                         $this->ping( $key ); // push to top
00088                         return $this->cache[$key][$prop];
00089                 } else {
00090                         return null;
00091                 }
00092         }
00093 
00100         public function clear( $keys = null ) {
00101                 if ( $keys === null ) {
00102                         $this->cache = array();
00103                 } else {
00104                         foreach ( (array)$keys as $key ) {
00105                                 unset( $this->cache[$key] );
00106                         }
00107                 }
00108         }
00109 
00115         protected function ping( $key ) {
00116                 $item = $this->cache[$key];
00117                 unset( $this->cache[$key] );
00118                 $this->cache[$key] = $item;
00119         }
00120 }