PHP Cache technology implementation

Source: Internet
Author: User
A php Cache Implementation is developed to implement apc and file caching. by inheriting Cache_Abstract, a third-party caching tool can be called. Refer to the cache class and apc of shindig. Php code & lt ;? PhpclassCacheExceptionextendsException {}/**... "> <LINKhref =" ht

A php Cache Implementation is developed to implement apc and file caching. by inheriting Cache_Abstract, a third-party caching tool can be called.

Refer to the cache class and apc of shindig.

Php code

  

Class CacheException extends Exception {}

/**

* Cache abstract class

*/

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 cached variable value

* @ 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) 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 );

/**

* Unlock cache variables

*

* @ Param string $ key cache subscript

* @ Return Cache_Abstract

*/

Abstract public function unlock ($ key );

/**

* Whether the cache variable is locked

*

* @ Param string $ key cache subscript

* @ Return bool

*/

Abstract public function isLocked ($ key );

/**

* Make sure that the instance is not locked.

* You can perform a maximum of $ tries sleep waiting for unlocking. if the time-out period expires, the system skips and unlocks the tries.

*

* @ 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); // you can perform a maximum of ten sleep tasks and wait for unlocking. if the time-out period is exceeded and unlocked

$ 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 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 a 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;

}

/**

* Cache unit unlocking

*

* @ 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 Meng Chun

* @ 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 ');

}

}

/**

* Getting cached 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 leakage, the cached file is in the PHP file format and" Start"

*

* @ 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 leakage, the cached file is in the PHP file format and" Start"

*

* @ Param string $ key: cache variable subscript

* @ Param string $ value the cached 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 ,'

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: delete ()");

}

$ CacheFile = self: _ getCacheFile ($ key );

If (! @ Unlink ($ cacheFile )){

Throw new CacheException ("Cache file cocould not be 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 ("cocould not make cache directory ");

}

}

}

// Set 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

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.