PHP caching technology implementation_PHP tutorial

WBOY
Release: 2016-07-13 10:38:31
Original
898 people have browsed it

Send me a PHP cache implementation that implements apc and file caching. By inheriting Cache_Abstract, you can call third-party caching tools.
Refer to shindig's cache class and apc.
Php code

  1. class CacheException extends Exception {}  
  2. /**
  3. * Cache abstract class
  4. ​*/  
  5. abstract class Cache_Abstract {  
  6.     /**
  7. * Read cache variables
  8. * *
  9. * @param string $key cache index
  10. * @return mixed
  11. ​​*/  
  12.     abstract public function fetch($key);  
  13.       
  14.     /**
  15. * Cache variables
  16. * *
  17. * @param string $key cache variable subscript
  18. * @param string $value The value of the cache variable
  19. * @return bool
  20. ​​*/  
  21.     abstract public function store($key, $value);  
  22.       
  23.     /**
  24. * Delete cache variables
  25. * *
  26. * @param string $key cache index
  27. * @return Cache_Abstract
  28. ​​*/  
  29.     abstract public function delete($key);  
  30.       
  31.     /**
  32. * Clear (delete) all caches
  33. * *
  34. * @return Cache_Abstract
  35. ​​*/  
  36.     abstract public function clear();  
  37.       
  38.     /**
  39. * Lock cache variables
  40. * *
  41. * @param string $key cache index
  42. * @return Cache_Abstract
  43. ​​*/  
  44.     abstract public function lock($key);  
  45.   
  46.     /**
  47. * Cache variable unlocking
  48. * *
  49. * @param string $key cache index
  50. * @return Cache_Abstract
  51. ​​*/  
  52.     abstract public function unlock($key);  
  53.   
  54.     /**
  55. * Get whether the cache variable is locked
  56. * *
  57. * @param string $key cache index
  58. * @return bool
  59. ​​*/  
  60.     abstract public function isLocked($key);  
  61.   
  62.     /**
  63. * Make sure it is not locked
  64. * * Sleep at most $tries times to wait for unlocking, skip and unlock when timeout
  65. * *
  66. * @param string $key cache index
  67. ​​*/  
  68.     public function checkLock($key) {  
  69. If (!$this->isLocked($key)) {
  70. Return $this;
  71.            } 
  72.                                                                         
  73. $tries = 10;
  74.         $count = 0;                                        
  75. do {
  76. usleep(200);
  77.                    $count ++;                                          
  78.                                                                                                                                                    
  79.            $this->isLocked($key) && $this->unlock($key); 
  80.                                                                         
  81.          return $this;                                  
  82. }  
  83. }
  84. /**
  85. * APC extended cache implementation
  86. *
  87. *
  88. * @category Mjie
  89. * @package Cache
  90. * @author Liushui Mengchun
  91. * @copyright Copyright (c) 2008-
  92. * @license New BSD License
  93. * @version $Id: Cache/Apc.php version number 2010-04-18 23:02 cmpan $
  94. ​*/
  95. class Cache_Apc extends Cache_Abstract {
  96.          
  97. Protected $_prefix = 'cache.mjie.net';
  98.          
  99. Public function __construct() {
  100. If (!function_exists('apc_cache_info')) {
  101. Throw new CacheException('apc extension didn't installed');
  102.            } 
  103. }  
  104.          
  105. /**
  106. * * Save cache variables
  107. * *
  108. * @param string $key
  109. * @param mixed $value
  110. * @return bool
  111. ​​*/
  112. Public function store($key, $value) {
  113.           return apc_store($this->_storageKey($key), $value);
  114. }  
  115.          
  116. /**
  117. * Read cache
  118. * *
  119. * @param string $key
  120. * @return mixed
  121. ​​*/
  122. Public function fetch($key) {
  123.           return apc_fetch($this->_storageKey($key)); 
  124. }  
  125.       
  126.     /**
  127. * Clear cache
  128. * *
  129. * @return Cache_Apc
  130. ​​*/  
  131.     public function clear() {  
  132.         apc_clear_cache();  
  133.         return $this;  
  134.     }  
  135.       
  136.     /**
  137. * Delete cache unit
  138. * *
  139. * @return Cache_Apc
  140. ​​*/  
  141.     public function delete($key) {  
  142.         apc_delete($this->_storageKey($key));  
  143.         return $this;  
  144.     }  
  145.       
  146.     /**
  147. * Whether the cache unit is locked
  148. * *
  149. * @param string $key
  150. * @return bool
  151. ​​*/  
  152.     public function isLocked($key) {  
  153.         if ((apc_fetch($this->_storageKey($key) . '.lock')) === false) {  
  154.             return false;  
  155.         }  
  156.           
  157.         return true;  
  158.     }  
  159.       
  160.     /**
  161. * Lock cache unit
  162. * *
  163. * @param string $key
  164. * @return Cache_Apc
  165. ​​*/  
  166.     public function lock($key) {  
  167.         apc_store($this->_storageKey($key) . '.lock', '', 5);  
  168.         return $this;  
  169.     }  
  170.       
  171.     /**
  172. * Cache unit unlocked
  173. * *
  174. * @param string $key
  175. * @return Cache_Apc
  176. ​​*/  
  177.     public function unlock($key) {  
  178.         apc_delete($this->_storageKey($key) . '.lock');  
  179.         return $this;  
  180.     }  
  181.       
  182.     /**
  183. * Full cache name
  184. * *
  185. * @param string $key
  186. * @return string
  187. ​​*/  
  188.     private function _storageKey($key) {  
  189.         return $this->_prefix . '_' . $key;  
  190.     }  
  191. }  
  192.   
  193. /**
  194. * File cache implementation
  195. *
  196. *
  197. * @category Mjie
  198. * @package Cache
  199. * @author Liushui Mengchun
  200. * @copyright Copyright (c) 2008-
  201. * @license New BSD License
  202. * @version $Id: Cache/File.php version number 2010-04-18 16:46 cmpan $
  203. ​*/  
  204. class Cache_File extends Cache_Abstract {  
  205.     public $useSubdir     = false;  
  206.       
  207.     protected $_cachesDir = 'cache';  
  208.       
  209.     public function __construct() {  
  210.         if (defined('DATA_DIR')) {  
  211.             $this->_setCacheDir(DATA_DIR . '/cache');  
  212.         }  
  213.     }  
  214.       
  215.     /**
  216. * Get cache files
  217. * *
  218. * @param string $key
  219. * @return string
  220. ​​*/  
  221.     protected function _getCacheFile($key) {  
  222.         $subdir = $this->useSubdir ? substr($key, 0, 2) . '/' : '';  
  223.         return $this->_cachesDir . '/' . $subdir . $key . '.php';  
  224.     }  
  225.   
  226.     /**
  227. * Read cache variables
  228. * To prevent information leakage, the cache file format is a php file and starts with ""
  229. * *
  230. * @param string $key cache index
  231. * @return mixed
  232. ​​*/  
  233.     public function fetch($key) {  
  234.         $cacheFile = self::_getCacheFile($key);  
  235.         if (file_exists($cacheFile) && is_readable($cacheFile)) {  
  236.             // include 方式  
  237.             //return include $cacheFile;  
  238.             // 系列化方式  
  239.   
  240.             return unserialize(@file_get_contents($cacheFile, false, NULL, 13));  
  241.         }  
  242.   
  243.         return false;  
  244.     }  
  245.   
  246.     /**
  247. * Cache variables
  248. * To prevent information leakage, the cache file format is a php file and starts with ""
  249. * *
  250. * @param string $key cache variable subscript
  251. * @param string $value The value of the cache variable
  252. * @return bool
  253. ​​*/  
  254.     public function store($key, $value) {  
  255.         $cacheFile = self::_getCacheFile($key);  
  256.         $cacheDir  = dirname($cacheFile);  
  257.   
  258.         if(!is_dir($cacheDir)) {  
  259.             if(!@mkdir($cacheDir, 0755, true)) {  
  260.                 throw new CacheException("Could not make cache directory");  
  261.             }  
  262.         }  
  263.     // 用include方式  
  264.         //return @file_put_contents($cacheFile, '
  265.   
  266.         return @file_put_contents($cacheFile, '' . serialize($value));  
  267.     }  
  268.   
  269.     /**
  270. * Delete cache variables
  271. * *
  272. * @param string $key cache index
  273. * @return Cache_File
  274. ​​*/  
  275.     public function delete($key) {  
  276.         if(emptyempty($key)) {  
  277.             throw new CacheException("Missing argument 1 for Cache_File::delete()");  
  278.         }  
  279.           
  280.         $cacheFile = self::_getCacheFile($key);  
  281.         if(!@unlink($cacheFile)) {  
  282.             throw new CacheException("Cache file could not be deleted");  
  283.         }  
  284.   
  285.         return $this;  
  286.     }  
  287.   
  288.     /**
  289. * Whether the cache unit has been locked
  290. * *
  291. * @param string $key
  292. * @return bool
  293. ​​*/  
  294.     public function isLocked($key) {  
  295.         $cacheFile = self::_getCacheFile($key);  
  296.         clearstatcache();  
  297.         return file_exists($cacheFile . '.lock');  
  298.     }  
  299.   
  300.     /** 
  301.      * 锁定 
  302.      * 
  303.      * @param string $key 
  304.      * @return Cache_File 
  305.      */  
  306.     public function lock($key) {  
  307.         $cacheFile = self::_getCacheFile($key);  
  308.         $cacheDir  = dirname($cacheFile);  
  309.         if(!is_dir($cacheDir)) {  
  310.             if(!@mkdir($cacheDir, 0755, true)) {  
  311.                 if(!is_dir($cacheDir)) {  
  312.                     throw new CacheException("Could not make cache directory");  
  313.                 }  
  314.             }  
  315.         }  
  316.   
  317.         // 设定缓存锁文件的访问和修改时间  
  318.         @touch($cacheFile . '.lock');  
  319.         return $this;  
  320.     }
  321.  
  322. /** 
  323.      * 解锁 
  324.      * 
  325.      * @param string $key 
  326.      * @return Cache_File 
  327.      */
  328. Public function unlock($key) {
  329.          $cacheFile = self::_getCacheFile($key);
  330.           @unlink($cacheFile . '.lock');
  331.         return 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/735086.htmlTechArticleSend a PHP cache implementation, which implements apc and file caching. By inheriting Cache_Abstract, you can call third-party caching tools. . Refer to shindig's cache class and apc. Php code ?php classCacheE...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template