Php supports apc and file cache classes-PHP source code

Source: Internet
Author: User
Php supports apc and file cache classes

1. [Code][PHP] code
 IsLocked ($ key) {return $ this;} $ tries = 10; $ count = 0; do {usleep (200); $ count ++ ;} while ($ count <= $ tries & $ this-> isLocked ($ key); // you can perform a maximum of ten sleep times and wait for unlocking, when timeout occurs, the system skips and unlocks $ this-> isLocked ($ key) & $ this-> unlock ($ key); return $ this ;}} /*** APC extension Cache implementation *** @ category Mjie * @ package Cache * @ author streamline Meng Chun * @ copyright Copyright (c) 2008-
 
  
* @ License New BSD License * @ version $ Id: Cache/Apc. php version 2010-04-18 cmpan $ */class Cache_Apc extends Cache_Abstract {protected $ _ prefix = 'cache .mjie.net '; public function _ construct () {if (! Function_exists ('apc _ cache_info ') {throw new CacheException ('apc extension didn' t installed ');}} /*** save the cache variable ** @ param string $ key * @ param mixed $ value * @ return bool */public function store ($ key, $ value) {return apc_store ($ this-> _ storageKey ($ key), $ value );} /*** read cache ** @ param string $ key * @ return mixed */public function fetch ($ key) {return apc_fetch ($ this-> _ storageKey ($ key);}/*** clear cache ** @ return Cache_Apc */public function clear () {apc_clear_cache (); return $ this;}/*** delete cache unit ** @ return Cache_Apc */public function delete ($ key) {apc_delete ($ this-> _ storageKey ($ key); return $ this ;} /*** whether the cache unit is locked ** @ param string $ key * @ return bool */public function isLocked ($ key) {if (apc_fetch ($ this-> _ storageKey ($ key ). '. lock') = false) {return false;} return true ;} /*** lock the cache unit ** @ param string $ key * @ return Cache_Apc */public function lock ($ key) {apc_store ($ this-> _ storageKey ($ key ). '. lock', '', 5); return $ this ;} /*** unlock the cache unit ** @ param string $ key * @ return Cache_Apc */public function unlock ($ key) {apc_delete ($ this-> _ storageKey ($ key ). '. lock'); return $ this;}/*** complete cache name ** @ param string $ key * @ return string */private function _ storageKey ($ key) {return $ this-> _ prefix. '_'. $ key ;}/ *** file Cache implementation *** @ category Mjie * @ package Cache * @ author streamline Mengchun * @ copyright Copyright (c) 2008-
  
   
* @ License New BSD License * @ version $ Id: Cache/File. php version cmpan $ */class Cache_File extends Cache_Abstract {public $ useSubdir = false; protected $ _ cachesDir = 'cache'; public function _ construct () {if (defined ('data _ dir') {$ this-> _ setCacheDir (DATA_DIR. '/cache') ;}}/*** get cache file ** @ param string $ key * @ return string */protected function _ getCacheFile ($ key) {$ subdir = $ this -> UseSubdir? Substr ($ key, 0, 2 ). '/': ''; return $ this-> _ cachesDir. '/'. $ subdir. $ key. '. php ';}/*** read cache variable * to prevent information leakage, the cache file format is php file, and"
   "Start with ** @ param string $ key cache subscript * @ return mixed */public function fetch ($ key) {$ cacheFile = self: _ getCacheFile ($ key ); if (file_exists ($ cacheFile) & is_readable ($ cacheFile) {// include method // return include $ cacheFile; // serialization method return unserialize (@ file_get_contents ($ cacheFile, false, NULL, 13);} return false;}/*** cache variable * to prevent information leakage, the cached file is in the PHP file format and"
   "Start with ** @ param string $ key: cache variable subscript * @ param string $ value: cache variable value * @ return bool */public function store ($ key, $ value) {$ cacheFile = self: _ getCacheFile ($ key); $ cacheDir = dirname ($ cacheFile); if (! Is_dir ($ cacheDir) {if (! @ Mkdir ($ cacheDir, 0755, true) {throw new CacheException ("cocould not make cache directory ");}} // use the include method // return @ file_put_contents ($ cacheFile ,'
   '. Serialize ($ value);}/*** delete cache variable ** @ param string $ key cache subscript * @ return Cache_File */public function delete ($ key) {if (emptyempty ($ key) {throw new CacheException ("Missing argument 1 for Cache_File: delete ()");} $ cacheFile = self :: _ getCacheFile ($ key); if (! @ Unlink ($ cacheFile) {throw new CacheException ("Cache file cocould not be deleted") ;}return $ this ;} /*** whether the cache unit has been locked ** @ param string $ key * @ return bool */public function isLocked ($ key) {$ cacheFile = self :: _ getCacheFile ($ key); clearstatcache (); return file_exists ($ cacheFile. '. lock');}/*** lock ** @ param string $ key * @ return Cache_File */public function lock ($ key) {$ cacheFile = self :: _ getCacheFile ($ Key); $ cacheDir = dirname ($ cacheFile); if (! Is_dir ($ cacheDir) {if (! @ Mkdir ($ cacheDir, 0755, true) {if (! Is_dir ($ cacheDir) {throw new CacheException ("cocould not make cache directory") ;}}// sets the access and modification time of the cache lock file @ touch ($ cacheFile. '. lock'); return $ this;}/*** unlock ** @ param string $ key * @ return Cache_File */public function unlock ($ key) {$ cacheFile = self:: _ getCacheFile ($ key); @ unlink ($ cacheFile. '. lock'); return $ this;}/*** sets the file cache directory * @ param string $ dir * @ return Cache_File */protected function _ setCacheDir ($ Dir) {$ this-> _ cachesDir = rtrim (str_replace ('\', '/', trim ($ dir), '/'); clearstatcache (); if (! Is_dir ($ this-> _ cachesDir) {mkdir ($ this-> _ cachesDir, 0755, true) ;}// return $ this ;} /*** clear all the caches ** @ return Cache_File */public function clear () {// clear the cache by traversing the directory $ cacheDir = $ this-> _ cachesDir; $ d = dir ($ cacheDir); while (false! ==( $ Entry = $ d-> read () {if ('. '= $ entry [0]) {continue;} $ cacheEntry = $ cacheDir. '/'. $ entry; if (is_file ($ cacheEntry) {@ unlink ($ cacheEntry);} elseif (is_dir ($ cacheEntry )) {// The cache folder has two levels: $ d2 = dir ($ cacheEntry); while (false! ==( $ Entry = $ d2-> read () {if ('. '= $ entry [0]) {continue;} $ cacheEntry. = '/'. $ entry; if (is_file ($ cacheEntry) {@ unlink ($ cacheEntry) ;}}$ d2-> close () ;}$ d-> close (); return $ this ;}/ *** data structure of the cache unit * array (* 'time' => time (), // cache write timestamp * 'expire '=> $ expire, // cache expiration time * 'valid' => true, // whether the Cache is valid * 'data' => $ value // Cache value *); */final class Cache {/*** Cache Expiration Time (s) ** @ var int */private $ _ Expire = 3600;/*** cache processing class ** @ var Cache_Abstract */private $ _ storage = null; /*** @ return Cache */static public function createCache ($ cacheClass = 'cache _ file') {return new self ($ cacheClass );} private function _ construct ($ cacheClass) {$ this-> _ storage = new $ cacheClass ();} /*** set cache ** @ param string $ key * @ param mixed $ value * @ param int $ expire */public function set ($ key, $ value, $ expire = False) {if (! $ Expire) {$ expire = $ this-> _ expire;} $ this-> _ storage-> checkLock ($ key ); $ data = array ('time' => time (), 'expire '=> $ expire, 'valid' => true, 'data' => $ value ); $ this-> _ storage-> lock ($ key); try {$ this-> _ storage-> store ($ key, $ data ); $ this-> _ storage-> unlock ($ key);} catch (CacheException $ e) {$ this-> _ storage-> unlock ($ key ); throw $ e ;}/ *** read cache ** @ param string $ key * @ return mixed */p Ublic function get ($ key) {$ data = $ this-> fetch ($ key); if ($ data & $ data ['valid'] &! $ Data ['isexpired']) {return $ data ['data'];} return false;}/*** read cache, including expired and invalid, obtain the complete storage structure ** @ param string $ key */public function fetch ($ key) {$ this-> _ storage-> checkLock ($ key ); $ data = $ this-> _ storage-> fetch ($ key); if ($ data) {$ data ['isexpired'] = (time () -$ data ['Time'])> $ data ['expire ']? True: false; return $ data;} return false;}/*** delete cache ** @ param string $ key */public function delete ($ key) {$ this-> _ storage-> checkLock ($ key)-> lock ($ key)-> delete ($ key)-> unlock ($ key );} public function clear () {$ this-> _ storage-> clear ();} /*** set the cache to invalid ** @ param string $ key */public function setInvalidate ($ key) {$ this-> _ storage-> checkLock ($ key) -> lock ($ key); try {$ data = $ this-> _ storage-> fetch ($ key); if ($ data) {$ data ['valid'] = false; $ this-> _ storage-> store ($ key, $ data );} $ this-> _ storage-> unlock ($ key);} catch (CacheException $ e) {$ this-> _ storage-> unlock ($ key ); throw $ e ;}/ *** set the cache Expiration Time (s) ** @ param int $ expire */public function setExpire ($ expire) {$ this-> _ expire = (int) $ expire; return $ this ;}}
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.