PHP cache implementation, the implementation of APC and file cache, inherited cache_abstract can be implemented to call third-party caching tools.
Refer to the Shindig cache class and APC.
PHP code
- Class Cacheexception extends Exception {}
- /**
- * Caching Abstract classes
- */
- Abstract class Cache_abstract {
- /**
- * Read Cache variables
- *
- * @param string $key cache subscript
- * @return Mixed
- */
- Abstract public Function fetch ($KEY);
- /**
- * Cache Variables
- *
- * @param string $key cache variable Subscript
- * @param string $value The value of the cache variable
- * @return BOOL
- */
- Abstract Public function Store ($key, $value);
- /**
- * Delete Cache variables
- *
- * @param string $key cache subscript
- * @return Cache_abstract
- */
- Abstract Public Function Delete ($key);
- /**
- * Clear (delete) except all caches
- *
- * @return Cache_abstract
- */
- Abstract public Function clear ();
- /**
- * Lock Cache variables
- *
- * @param string $key cache subscript
- * @return Cache_abstract
- */
- Abstract Public Function lock ($key);
- /**
- * Cache Variable Unlocked
- *
- * @param string $key cache subscript
- * @return Cache_abstract
- */
- Abstract public Function unlock ($key);
- /**
- * Gets whether the cache variable is locked
- *
- * @param string $key cache subscript
- * @return BOOL
- */
- Abstract public Function isLocked ($key);
- /**
- * Make sure not locked status
- * Up to $tries sleep waiting to be unlocked, timeout skipped and unlocked
- *
- * @param string $key cache subscript
- */
- Public Function Checklock ($key) {
- if (! $this->islocked ($key)) {
- return $this;
- }
- $tries = 10;
- $count = 0;
- do {
- Usleep (200);
- $count + +;
- } while ($count <= $tries && $this->islocked ($key)); Do up to 10 sleep waits to unlock, timeout to skip and unlock
- $this->islocked ($key) && $this->unlock ($key);
- return $this;
- }
- }
- /**
- * APC Extended Cache implementation
- *
- *
- * @category Mjie
- * @package Cache
- * @author Water Mengchun
- * @copyright Copyright (c) 2008-
- * @license New BSD License
- * @version $Id: cache/apc.php version number 2010-04-18 23:02 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 Cache variables
- *
- * @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 Cache Unit
- *
- * @param string $key
- * @return CACHE_APC
- */
- Public function Lock ($key) {
- Apc_store ($this->_storagekey ($key). '. Lock ', ', 5 ';
- return $this;
- }
- /**
- * Cache Unit Unlocked
- *
- * @param string $key
- * @return CACHE_APC
- */
- Public function Unlock ($key) {
- Apc_delete ($this->_storagekey ($key). '. Lock ');
- return $this;
- }
- /**
- * Full Cache Name
- *
- * @param string $key
- * @return String
- */
- Private Function _storagekey ($key) {
- Return $this->_prefix. '_' . $key;
- }
- }
- /**
- * File Cache implementation
- *
- *
- * @category Mjie
- * @package Cache
- * @author Water Mengchun
- * @copyright Copyright (c) 2008-
- * @license New BSD License
- * @version $Id: cache/file.php version number 2010-04-18 16:46 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 files
- *
- * @param string $key
- * @return String
- */
- protected function _getcachefile ($key) {
- $subdir = $this->usesubdir? substr ($key, 0, 2). '/' : '';
- Return $this->_cachesdir. '/' . $subdir. $key. '. php ';
- }
- /**
- * Read Cache variables
- * To prevent information disclosure, the cache file format is PHP file and begins 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 Variables
- * To prevent information disclosure, the cache file format is PHP file and begins with " "
- *
- * @param string $key cache variable Subscript
- * @param string $value The value of the cache variable
- * @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 ("Could not make cache directory");
- }
- }
- Use the Include method
- Return @file_put_contents ($cacheFile, '
- Return @file_put_contents ($cacheFile, " . Serialize ($value));
- }
- /**
- * Delete Cache variables
- *
- * @param string $key cache subscript
- * @return Cache_file
- */
- Public Function Delete ($key) {
- if (Emptyempty ($key)) {
- throw new Cacheexception ("Missing argument 1 for Cache_file::d elete ()");
- }
- $cacheFile = Self::_getcachefile ($key);
- if (! @unlink ($cacheFile)) {
- throw new Cacheexception ("Cache file could not being deleted");
- }
- return $this;
- }
- /**
- * Whether the cache unit is 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 ("Could not make cache directory");
- }
- }
- }
- Setting access and modification times for cache lock files
- @touch ($cacheFile. '. Lock ');
- return $this;
- }
- /**
- * Unlock
- *
- * @param string $key
- * @return Cache_file
- */
- Public function Unlock ($key) {
- $cacheFile = Self::_getcachefile ($key);
- @unlink ($cacheFile. '. Lock ');
- Return
http://www.bkjia.com/PHPjc/735086.html www.bkjia.com true http://www.bkjia.com/PHPjc/735086.html techarticle PHP Cache Implementation, the implementation of APC and file cache, inherited cache_abstract can be implemented to call third-party caching tools. Refer to the Shindig cache class and APC. PHP code? PHP Classcachee ...