[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/ -> Cache.php (source)

   1  <?php
   2  /**
   3   * The Horde_Cache:: class provides a common abstracted interface into
   4   * the various caching backends.  It also provides functions for
   5   * checking in, retrieving, and flushing a cache.
   6   *
   7   * $Horde: framework/Cache/Cache.php,v 1.31.10.11 2006/01/01 21:28:10 jan Exp $
   8   *
   9   * Copyright 1999-2006 Anil Madhavapeddy <anil@recoil.org>
  10   * Copyright 1999-2006 Chuck Hagenbuch <chuck@horde.org>
  11   *
  12   * See the enclosed file COPYING for license information (LGPL). If you
  13   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  14   *
  15   * @author  Anil Madhavapeddy <anil@recoil.org>
  16   * @author  Chuck Hagenbuch <chuck@horde.org>
  17   * @since   Horde 1.3
  18   * @package Horde_Cache
  19   */
  20  class Horde_Cache {
  21  
  22      /**
  23       * Construct a new Horde_Cache object.
  24       *
  25       * @param array $params  Parameter array.
  26       */
  27      function Horde_Cache($params = array())
  28      {
  29          /* Only do garbage collection if asked for, and then only 1% of the
  30           * time we create an object. */
  31          if (!empty($params['gc']) &&
  32              (rand(0, 99) == 0)) {
  33              $this->_doGC($params['gc']);
  34          }
  35      }
  36  
  37      /**
  38       * Attempts to retrieve a cached object and return it to the
  39       * caller.
  40       *
  41       * @abstract
  42       *
  43       * @param string  $key       Object ID to query.
  44       * @param integer $lifetime  Lifetime of the object in seconds.
  45       *
  46       * @return mixed  Cached data, or false if none was found.
  47       */
  48      function get($key, $lifetime = 1)
  49      {
  50          return false;
  51      }
  52  
  53      /**
  54       * Attempts to store an object in the cache.
  55       *
  56       * @abstract
  57       *
  58       * @param string $key   Object ID used as the caching key.
  59       * @param mixed  $data  Data to store in the cache.
  60       *
  61       * @return boolean  True on success, false on failure.
  62       */
  63      function set($key, $data)
  64      {
  65          return true;
  66      }
  67  
  68      /**
  69       * Attempts to directly output a cached object.
  70       *
  71       * @abstract
  72       *
  73       * @param string  $key       Object ID to query.
  74       * @param integer $lifetime  Lifetime of the object in seconds.
  75       *
  76       * @return mixed  Cached data, or false if none was found.
  77       */
  78      function output($key, $lifetime = 1)
  79      {
  80          return false;
  81      }
  82  
  83      /**
  84       * Checks if a given key exists in the cache, valid for the given
  85       * lifetime.
  86       *
  87       * @abstract
  88       *
  89       * @param string  $key       Cache key to check.
  90       * @param integer $lifetime  Lifetime of the key in seconds.
  91       *
  92       * @return boolean  Existance.
  93       */
  94      function exists($key, $lifetime = 1)
  95      {
  96          return false;
  97      }
  98  
  99      /**
 100       * Expire any existing data for the given key.
 101       *
 102       * @abstract
 103       *
 104       * @param string $key  Cache key to expire.
 105       *
 106       * @return boolean  Success or failure.
 107       */
 108      function expire($key)
 109      {
 110          return true;
 111      }
 112  
 113      /**
 114       * Do any garbage collection needed for the driver.
 115       *
 116       * @private
 117       *
 118       * @abstract
 119       *
 120       * @param integer $secs  The minimum amount of time (in seconds) required
 121       *                       before a cache item is removed.
 122       */
 123      function _doGC($secs)
 124      {
 125      }
 126  
 127      /**
 128       * Attempts to return a concrete Horde_Cache instance based on $driver.
 129       *
 130       * @param mixed $driver  The type of concrete Horde_Cache subclass to
 131       *                       return. If $driver is an array, then we will look
 132       *                       in $driver[0]/lib/Cache/ for the subclass
 133       *                       implementation named $driver[1].php.
 134       * @param array $params  A hash containing any additional
 135       *                       configuration or connection parameters a subclass
 136       *                       might need.
 137       *
 138       * @return Horde_Cache  The newly created concrete Horde_Cache instance, or
 139       *                      false on error.
 140       */
 141      function &factory($driver, $params = array())
 142      {
 143          if (is_array($driver)) {
 144              $app = $driver[0];
 145              $driver = $driver[1];
 146          }
 147  
 148          $driver = basename($driver);
 149          if (empty($driver) || $driver == 'none') {
 150              $cache = &new Horde_Cache($params);
 151              return $cache;
 152          }
 153  
 154          if (!empty($app)) {
 155              include_once $app . '/lib/Cache/' . $driver . '.php';
 156          } elseif (@file_exists(dirname(__FILE__) . '/Cache/' . $driver . '.php')) {
 157              include_once dirname(__FILE__) . '/Cache/' . $driver . '.php';
 158          } else {
 159              @include_once 'Horde/Cache/' . $driver . '.php';
 160          }
 161  
 162          $class = 'Horde_Cache_' . $driver;
 163          if (class_exists($class)) {
 164              $cache = &new $class($params);
 165          } else {
 166              $cache = PEAR::raiseError('Class definition of ' . $class . ' not found.');
 167          }
 168  
 169          return $cache;
 170      }
 171  
 172      /**
 173       * Attempts to return a reference to a concrete Horde_Cache
 174       * instance based on $driver. It will only create a new instance
 175       * if no Horde_Cache instance with the same parameters currently
 176       * exists.
 177       *
 178       * This should be used if multiple cache backends (and, thus,
 179       * multiple Horde_Cache instances) are required.
 180       *
 181       * This method must be invoked as:
 182       * $var = &Horde_Cache::singleton()
 183       *
 184       * @param mixed $driver  The type of concrete Horde_Cache subclass to
 185       *                       return. If $driver is an array, then we will look
 186       *                       in $driver[0]/lib/Cache/ for the subclass
 187       *                       implementation named $driver[1].php.
 188       * @param array $params  A hash containing any additional configuration or
 189       *                       connection parameters a subclass might need.
 190       *
 191       * @return Horde_Cache  The concrete Horde_Cache reference, or false on
 192       *                      error.
 193       */
 194      function &singleton($driver, $params = array())
 195      {
 196          static $instances = array();
 197  
 198          $signature = serialize(array($driver, $params));
 199          if (empty($instances[$signature])) {
 200              $instances[$signature] = &Horde_Cache::factory($driver, $params);
 201          }
 202  
 203          return $instances[$signature];
 204      }
 205  
 206  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7