[ Index ]
 

Code source de Symfony 1.0.0

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

title

Body

[fermer]

/lib/cache/ -> sfProcessCache.class.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the symfony package.
   5   * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
   6   * 
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * sfProcessCache stores content in memory if you run a PHP accelerator.
  13   *
  14   * Current PHP accelerator supported: APC, XCache and Eaccelerator.
  15   *
  16   * @package    symfony
  17   * @subpackage cache
  18   * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
  19   * @version    SVN: $Id$
  20   */
  21  class sfProcessCache
  22  {
  23    /**
  24     * Gets the cache engine name or false if no PHP accelerator is enabled.
  25     *
  26     * @return string The cache engine name
  27     */
  28    public static function cacher()
  29    {
  30      static $cacher = null;
  31  
  32      if (null === $cacher)
  33      {
  34        if (!sfConfig::get('sf_use_process_cache'))
  35        {
  36          $cacher = false;
  37        }
  38        elseif (function_exists('apc_store'))
  39        {
  40          $cacher = 'apc';
  41        }
  42        elseif (function_exists('xcache_set'))
  43        {
  44          $cacher = 'xcache';
  45        }
  46        elseif (function_exists('ecacher_put'))
  47        {
  48          $cacher = 'eaccelerator';
  49        }
  50        else
  51        {
  52          $cacher = false;
  53        }
  54      }
  55  
  56      return $cacher;
  57    }
  58  
  59    /**
  60     * Gets the prefix to use for all key name.
  61     *
  62     * @return string The prefix string
  63     */
  64    public static function getPrefix()
  65    {
  66      static $prefix = null;
  67  
  68      if (!$prefix)
  69      {
  70        $prefix = md5(sfConfig::get('sf_app_dir')).'_';
  71      }
  72  
  73      return $prefix;
  74    }
  75  
  76    /**
  77     * Sets a value in the cache for the specified key.
  78     *
  79     * @param string The key name
  80     * @param string The content to put in cache
  81     * @param int The life time to keep the content in the cache
  82     *
  83     * @return boolean true if ok
  84     */
  85    public static function set($key, $value, $lifeTime = 0)
  86    {
  87      switch (self::cacher())
  88      {
  89        case 'apc':
  90          return apc_store(self::getPrefix().$key, $value, $lifeTime);
  91        case 'xcache':
  92          return xcache_set(self::getPrefix().$key, $value, $lifeTime);
  93        case 'eaccelerator':
  94          return eaccelerator_put(self::getPrefix().$key, serialize($value), $lifeTime);
  95      }
  96  
  97      return false;
  98    }
  99  
 100    /**
 101     * Gets a value in the cache for the specified key.
 102     *
 103     * @param string The key name
 104     *
 105     * @return mixed The content associated with the key or null if the key does not exist
 106     */
 107    public static function get($key)
 108    {
 109      switch (self::cacher())
 110      {
 111        case 'apc':
 112          $value = apc_fetch(self::getPrefix().$key);
 113          return false === $value ? null : $value;
 114        case 'xcache':
 115          return xcache_isset(self::getPrefix().$key) ? xcache_get(self::getPrefix().$key) : null;
 116        case 'eaccelerator':
 117          return unserialize(eaccelerator_get(self::getPrefix().$key));
 118      }
 119  
 120      return null;
 121    }
 122  
 123    /**
 124     * Returns true if a given key exists in the cache, false otherwise.
 125     *
 126     * @param string The key name
 127     *
 128     * @return boolean true if the key exists, false otherwise
 129     */
 130    public static function has($key)
 131    {
 132      switch (self::cacher())
 133      {
 134        case 'apc':
 135          return false === apc_fetch(self::getPrefix().$key) ? false : true;
 136        case 'xcache':
 137          return xcache_isset(self::getPrefix().$key);
 138        case 'eaccelerator':
 139          return null === eaccelerator_get(self::getPrefix().$key) ? false : true;
 140      }
 141  
 142      return false;
 143    }
 144  
 145    /**
 146     * Clears the cache.
 147     *
 148     * @return boolean true if ok, false otherwise
 149     */
 150    public static function clear()
 151    {
 152      switch (self::cacher())
 153      {
 154        case 'apc':
 155          return apc_clear_cache('user');
 156        case 'xcache':
 157          for ($i = 0, $max = xcache_count(XC_TYPE_VAR); $i < $max; $i++)
 158          {
 159            if (!xcache_clear_cache(XC_TYPE_VAR, $i))
 160            {
 161              return false;
 162            }
 163          }
 164          return true;
 165        case 'eaccelerator':
 166          eaccelerator_clean();
 167      }
 168  
 169      return false;
 170    }
 171  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7