[ Index ]
 

Code source de Horde 3.1.3

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

title

Body

[fermer]

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

   1  <?php
   2  /**
   3   * The Horde_Cache_file:: class provides a filesystem implementation of the
   4   * Horde caching system.
   5   *
   6   * Optional parameters:<pre>
   7   *   'dir'     The directory to store the cache files in.
   8   *   'prefix'  The filename prefix to use for the cache files.</pre>
   9   *
  10   * $Horde: framework/Cache/Cache/file.php,v 1.28.10.9 2006/01/01 21:28:10 jan Exp $
  11   *
  12   * Copyright 1999-2006 Anil Madhavapeddy <anil@recoil.org>
  13   * Copyright 1999-2006 Chuck Hagenbuch <chuck@horde.org>
  14   *
  15   * See the enclosed file COPYING for license information (LGPL). If you
  16   * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  17   *
  18   * @author  Anil Madhavapeddy <anil@recoil.org>
  19   * @author  Chuck Hagenbuch <chuck@horde.org>
  20   * @since   Horde 1.3
  21   * @package Horde_Cache
  22   */
  23  class Horde_Cache_file extends Horde_Cache {
  24  
  25      /**
  26       * The location of the temp directory.
  27       *
  28       * @var string
  29       */
  30      var $_dir;
  31  
  32      /**
  33       * The filename prefix for cache files.
  34       *
  35       * @var string
  36       */
  37      var $_prefix = 'cache_';
  38  
  39      /**
  40       * Construct a new Horde_Cache_file object.
  41       *
  42       * @param array $params  Parameter array.
  43       */
  44      function Horde_Cache_file($params = array())
  45      {
  46          if (!empty($params['dir']) && @is_dir($params['dir'])) {
  47              $this->_dir = $params['dir'];
  48          } else {
  49              require_once  'Horde/Util.php';
  50              $this->_dir = Util::getTempDir();
  51          }
  52  
  53          if (isset($params['prefix'])) {
  54              $this->_prefix = $params['prefix'];
  55          }
  56  
  57          parent::Horde_Cache($params);
  58      }
  59  
  60      /**
  61       * Attempts to retrieve cached data from the filesystem and return it to
  62       * the caller.
  63       *
  64       * @param string  $key       Cache key to fetch.
  65       * @param integer $lifetime  Lifetime of the data in seconds.
  66       *
  67       * @return mixed  Cached data, or false if none was found.
  68       */
  69      function get($key, $lifetime = 1)
  70      {
  71          if ($this->exists($key, $lifetime)) {
  72              $filename = $this->_keyToFile($key);
  73              $size = filesize($filename);
  74              if (!$size) {
  75                  return '';
  76              }
  77              $data = file_get_contents($filename);
  78              if ($data !== false) {
  79                  return $data;
  80              }
  81          }
  82  
  83          /* Nothing cached, return failure. */
  84          return false;
  85      }
  86  
  87      /**
  88       * Attempts to store data to the filesystem.
  89       *
  90       * @param string $key   Cache key.
  91       * @param mixed  $data  Data to store in the cache.
  92       *
  93       * @return boolean  True on success, false on failure.
  94       */
  95      function set($key, $data)
  96      {
  97          require_once  'Horde/Util.php';
  98          $filename = $this->_keyToFile($key);
  99          $tmp_file = Util::getTempFile('HordeCache', true, $this->_dir);
 100  
 101          if ($fd = fopen($tmp_file, 'w')) {
 102              if (fwrite($fd, $data) < strlen($data)) {
 103                  fclose($fd);
 104                  return false;
 105              } else {
 106                  fclose($fd);
 107                  @rename($tmp_file, $filename);
 108              }
 109          }
 110      }
 111  
 112      /**
 113       * Attempts to directly output cached data from the filesystem.
 114       *
 115       * @param string  $key       Cache key to output.
 116       * @param integer $lifetime  Lifetime of the data in seconds.
 117       *
 118       * @return mixed  Cached data, or false if none was found.
 119       */
 120      function output($key, $lifetime = 1)
 121      {
 122          if ($this->exists($key, $lifetime)) {
 123              $filename = $this->_keyToFile($key);
 124              $fd = @fopen($filename, 'r');
 125              if ($fd) {
 126                  fpassthru($fd);
 127                  return true;
 128              }
 129          }
 130  
 131          /* Nothing cached, return failure. */
 132          return false;
 133      }
 134  
 135      /**
 136       * Checks if a given key exists in the cache, valid for the given
 137       * lifetime. If it exists but is expired, delete the file.
 138       *
 139       * @param string  $key       Cache key to check.
 140       * @param integer $lifetime  Lifetime of the key in seconds.
 141       *
 142       * @return boolean  Existance.
 143       */
 144      function exists($key, $lifetime = 1)
 145      {
 146          $filename = $this->_keyToFile($key);
 147  
 148          /* Key exists in the cache */
 149          if (file_exists($filename)) {
 150              /* 0 means no expire. */
 151              if ($lifetime == 0) {
 152                  return true;
 153              }
 154  
 155              /* If the file was been created after the supplied value,
 156               * the data is valid (fresh). */
 157              if (time() - $lifetime <= filemtime($filename)) {
 158                  return true;
 159              } else {
 160                  @unlink($filename);
 161              }
 162          }
 163  
 164          return false;
 165      }
 166  
 167      /**
 168       * Expire any existing data for the given key.
 169       *
 170       * @param string $key  Cache key to expire.
 171       *
 172       * @return boolean  Success or failure.
 173       */
 174      function expire($key)
 175      {
 176          $filename = $this->_keyToFile($key);
 177          return @unlink($filename);
 178      }
 179  
 180      /**
 181       * Map a cache key to a unique filename.
 182       *
 183       * @access private
 184       *
 185       * @param string $key  Cache key.
 186       *
 187       * @return string  Fully qualified filename.
 188       */
 189      function _keyToFile($key)
 190      {
 191          return $this->_dir . '/' . $this->_prefix . md5($key);
 192      }
 193  
 194      /**
 195       * Do any garbage collection needed for the driver.
 196       *
 197       * @private
 198       *
 199       * @param integer $secs  The minimum amount of time (in seconds) required
 200       *                       before a cache item is removed.
 201       */
 202      function _doGC($secs)
 203      {
 204          $filename = $this->_dir . '/horde_cache_gc';
 205          $gc_time = time() - $secs;
 206          if (file_exists($filename)) {
 207              $old_time = file_get_contents($filename);
 208              if (($old_time !== false) &&
 209                  ($gc_time > $old_time)) {
 210                  return;
 211              }
 212          }
 213  
 214          $d = dir($this->_dir);
 215          while (($entry = $d->read()) !== false) {
 216              if (strpos($entry, $this->_prefix) === 0) {
 217                  $mtime = filemtime($this->_dir . '/' . $entry);
 218                  if ($gc_time > $mtime) {
 219                      @unlink($this->_dir . '/' . $entry);
 220                  }
 221              }
 222          }
 223          $d->close();
 224   
 225          $fp = fopen($filename, 'w');
 226          fwrite($fp, time());
 227          fclose($fp);
 228      }
 229  
 230  }


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