[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/libs/ -> ProcessCacheLRU.php (source)

   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          $this->resize( $maxKeys );
  42      }
  43  
  44      /**
  45       * Set a property field for a cache entry.
  46       * This will prune the cache if it gets too large based on LRU.
  47       * If the item is already set, it will be pushed to the top of the cache.
  48       *
  49       * @param $key string
  50       * @param $prop string
  51       * @param $value mixed
  52       * @return void
  53       */
  54  	public function set( $key, $prop, $value ) {
  55          if ( isset( $this->cache[$key] ) ) {
  56              $this->ping( $key ); // push to top
  57          } elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
  58              reset( $this->cache );
  59              $evictKey = key( $this->cache );
  60              unset( $this->cache[$evictKey] );
  61              unset( $this->cacheTimes[$evictKey] );
  62          }
  63          $this->cache[$key][$prop] = $value;
  64          $this->cacheTimes[$key][$prop] = time();
  65      }
  66  
  67      /**
  68       * Check if a property field exists for a cache entry.
  69       *
  70       * @param $key string
  71       * @param $prop string
  72       * @param $maxAge integer Ignore items older than this many seconds (since 1.21)
  73       * @return bool
  74       */
  75  	public function has( $key, $prop, $maxAge = 0 ) {
  76          if ( isset( $this->cache[$key][$prop] ) ) {
  77              return ( $maxAge <= 0 || ( time() - $this->cacheTimes[$key][$prop] ) <= $maxAge );
  78          }
  79  
  80          return false;
  81      }
  82  
  83      /**
  84       * Get a property field for a cache entry.
  85       * This returns null if the property is not set.
  86       * If the item is already set, it will be pushed to the top of the cache.
  87       *
  88       * @param $key string
  89       * @param $prop string
  90       * @return mixed
  91       */
  92  	public function get( $key, $prop ) {
  93          if ( isset( $this->cache[$key][$prop] ) ) {
  94              $this->ping( $key ); // push to top
  95              return $this->cache[$key][$prop];
  96          } else {
  97              return null;
  98          }
  99      }
 100  
 101      /**
 102       * Clear one or several cache entries, or all cache entries
 103       *
 104       * @param $keys string|Array
 105       * @return void
 106       */
 107  	public function clear( $keys = null ) {
 108          if ( $keys === null ) {
 109              $this->cache = array();
 110              $this->cacheTimes = array();
 111          } else {
 112              foreach ( (array)$keys as $key ) {
 113                  unset( $this->cache[$key] );
 114                  unset( $this->cacheTimes[$key] );
 115              }
 116          }
 117      }
 118  
 119      /**
 120       * Resize the maximum number of cache entries, removing older entries as needed
 121       *
 122       * @param $maxKeys integer
 123       * @return void
 124       */
 125  	public function resize( $maxKeys ) {
 126          if ( !is_int( $maxKeys ) || $maxKeys < 1 ) {
 127              throw new UnexpectedValueException( __METHOD__ . " must be given an integer >= 1" );
 128          }
 129          $this->maxCacheKeys = $maxKeys;
 130          while ( count( $this->cache ) > $this->maxCacheKeys ) {
 131              reset( $this->cache );
 132              $evictKey = key( $this->cache );
 133              unset( $this->cache[$evictKey] );
 134              unset( $this->cacheTimes[$evictKey] );
 135          }
 136      }
 137  
 138      /**
 139       * Push an entry to the top of the cache
 140       *
 141       * @param $key string
 142       */
 143  	protected function ping( $key ) {
 144          $item = $this->cache[$key];
 145          unset( $this->cache[$key] );
 146          $this->cache[$key] = $item;
 147      }
 148  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1