Advertisement
Neo-Craft

CFileCache class file

Jan 4th, 2014
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.52 KB | None | 0 0
  1. <?php
  2. /**
  3.  * CFileCache class file
  4.  *
  5.  * @author Qiang Xue <qiang.xue@gmail.com>
  6.  * @link http://www.yiiframework.com/
  7.  * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  8.  * @license http://www.yiiframework.com/license/
  9.  */
  10. class Cache
  11. {
  12.     /**
  13.      * @var string the directory to store cache files. Defaults to null, meaning
  14.      * using 'protected/runtime/cache' as the directory.
  15.      */
  16.     public $cachePath;
  17.     /**
  18.      * @var string cache file suffix. Defaults to '.bin'.
  19.      */
  20.     public $cacheFileSuffix='.bin';
  21.     /**
  22.      * @var integer the level of sub-directories to store cache files. Defaults to 0,
  23.      * meaning no sub-directories. If the system has huge number of cache files (e.g. 10K+),
  24.      * you may want to set this value to be 1 or 2 so that the file system is not over burdened.
  25.      * The value of this property should not exceed 16 (less than 3 is recommended).
  26.      */
  27.     public $directoryLevel=0;
  28.  
  29.     private $_gcProbability=100;
  30.     private $_gced=false;
  31.  
  32.     /** @return mixed **/
  33.     public function get($key)
  34.     {
  35.         return unserialize($this->getValue(sha1($key)));
  36.     }
  37.    
  38.     public function remove($key){
  39.         $this->deleteValue(sha1($key));
  40.     }
  41.    
  42.     /** @return bool **/
  43.     public function set($key,$value,$expire=3600)
  44.     {
  45.         return $this->setValue(sha1($key), serialize($value), $expire);
  46.     }
  47.    
  48.     /**
  49.      * Initializes this application component.   
  50.      * It checks the availability of memcache.   
  51.      */
  52.     public function init()
  53.     {
  54.         if($this->cachePath===null)
  55.             $this->cachePath=CACHE_DIR;
  56.         if(!is_dir($this->cachePath))
  57.             mkdir($this->cachePath,0777,true);
  58.     }
  59.  
  60.     /**
  61.      * @return integer the probability (parts per million) that garbage collection (GC) should be performed
  62.      * when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
  63.      */
  64.     public function getGCProbability()
  65.     {
  66.         return $this->_gcProbability;
  67.     }
  68.  
  69.     /**
  70.      * @param integer $value the probability (parts per million) that garbage collection (GC) should be performed
  71.      * when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
  72.      * This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
  73.      */
  74.     public function setGCProbability($value)
  75.     {
  76.         $value=(int)$value;
  77.         if($value<0)
  78.             $value=0;
  79.         if($value>1000000)
  80.             $value=1000000;
  81.         $this->_gcProbability=$value;
  82.     }
  83.  
  84.     /**
  85.      * Deletes all values from cache.
  86.      * This is the implementation of the method declared in the parent class.
  87.      * @return boolean whether the flush operation was successful.
  88.      * @since 1.1.5
  89.      */
  90.     protected function flushValues()
  91.     {
  92.         $this->gc(false);
  93.         return true;
  94.     }
  95.  
  96.     /**
  97.      * Retrieves a value from cache with a specified key.
  98.      * This is the implementation of the method declared in the parent class.
  99.      * @param string $key a unique key identifying the cached value
  100.      * @return string the value stored in cache, false if the value is not in the cache or expired.
  101.      */
  102.      
  103.        
  104.     protected function getValue($key)
  105.     {
  106.         $cacheFile=$this->getCacheFile($key);
  107.         if(($time=@filemtime($cacheFile))>time())
  108.             return @file_get_contents($cacheFile);
  109.         else if($time>0)
  110.             @unlink($cacheFile);
  111.         return false;
  112.     }
  113.  
  114.     /**
  115.      * Stores a value identified by a key in cache.
  116.      * This is the implementation of the method declared in the parent class.
  117.      *
  118.      * @param string $key the key identifying the value to be cached
  119.      * @param string $value the value to be cached
  120.      * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  121.      * @return boolean true if the value is successfully stored into cache, false otherwise
  122.      */
  123.     protected function setValue($key,$value,$expire)
  124.     {
  125.         if(!$this->_gced && mt_rand(0,1000000)<$this->_gcProbability)
  126.         {
  127.             $this->gc();
  128.             $this->_gced=true;
  129.         }
  130.  
  131.         if($expire<=0)
  132.             $expire=31536000; // 1 year
  133.         $expire+=time();
  134.  
  135.         $cacheFile=$this->getCacheFile($key);
  136.         if($this->directoryLevel>0)
  137.             @mkdir(dirname($cacheFile),0777,true);
  138.         if(@file_put_contents($cacheFile,$value,LOCK_EX)!==false)
  139.         {
  140.             @chmod($cacheFile,0777);
  141.             return @touch($cacheFile,$expire);
  142.         }
  143.         else
  144.             return false;
  145.     }
  146.  
  147.     /**
  148.      * Stores a value identified by a key into cache if the cache does not contain this key.
  149.      * This is the implementation of the method declared in the parent class.
  150.      *
  151.      * @param string $key the key identifying the value to be cached
  152.      * @param string $value the value to be cached
  153.      * @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
  154.      * @return boolean true if the value is successfully stored into cache, false otherwise
  155.      */
  156.     protected function addValue($key,$value,$expire)
  157.     {
  158.         $cacheFile=$this->getCacheFile($key);
  159.         if(@filemtime($cacheFile)>time())
  160.             return false;
  161.         return $this->setValue($key,$value,$expire);
  162.     }
  163.  
  164.     /**
  165.      * Deletes a value with the specified key from cache
  166.      * This is the implementation of the method declared in the parent class.
  167.      * @param string $key the key of the value to be deleted
  168.      * @return boolean if no error happens during deletion
  169.      */
  170.     protected function deleteValue($key)
  171.     {
  172.         $cacheFile=$this->getCacheFile($key);
  173.         return @unlink($cacheFile);
  174.     }
  175.  
  176.     /**
  177.      * Returns the cache file path given the cache key.
  178.      * @param string $key cache key
  179.      * @return string the cache file path
  180.      */
  181.     protected function getCacheFile($key)
  182.     {
  183.         if($this->directoryLevel>0)
  184.         {
  185.             $base=$this->cachePath;
  186.             for($i=0;$i<$this->directoryLevel;++$i)
  187.             {
  188.                 if(($prefix=substr($key,$i+$i,2))!==false)
  189.                     $base.=DIRECTORY_SEPARATOR.$prefix;
  190.             }
  191.             return $base.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
  192.         }
  193.         else
  194.             return $this->cachePath.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
  195.     }
  196.  
  197.     /**
  198.      * Removes expired cache files.
  199.      * @param boolean $expiredOnly whether to removed expired cache files only. If true, all cache files under {@link cachePath} will be removed.
  200.      * @param string $path the path to clean with. If null, it will be {@link cachePath}.
  201.      */
  202.     public function gc($expiredOnly=true,$path=null)
  203.     {
  204.         if($path===null)
  205.             $path=$this->cachePath;
  206.         if(($handle=opendir($path))===false)
  207.             return;
  208.         while(($file=readdir($handle))!==false)
  209.         {
  210.             if($file[0]==='.')
  211.                 continue;
  212.             $fullPath=$path.DIRECTORY_SEPARATOR.$file;
  213.             if(is_dir($fullPath))
  214.                 $this->gc($expiredOnly,$fullPath);
  215.             else if($expiredOnly && @filemtime($fullPath)<time() || !$expiredOnly)
  216.                 @unlink($fullPath);
  217.         }
  218.         closedir($handle);
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement