PHP Caching Technology Implementation

Source: Internet
Author: User

A PHP cache implementation, the implementation of APC and file caching, inheritance Cache_abstract can be implemented to invoke a Third-party caching tool.

Refer to the Shindig cache class and APC.

PHP code

<?php

Class Cacheexception extends Exception {}

/**

* Cache abstract Class

*/

Abstract class Cache_abstract {

/**

* Read Cache variable

*

* @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 cached variable

* @return BOOL

*/

Abstract Public function Store ($key, $value);

/**

* Delete Cache variable

*

* @param string $key cache subscript

* @return Cache_abstract

*/

Abstract Public Function Delete ($key);

/**

* Clear (delete) except for all caches

*

* @return Cache_abstract

*/

Abstract public Function clear ();

/**

* Lock Cache variable

*

* @param string $key cache subscript

* @return Cache_abstract

*/

Abstract Public Function lock ($key);

/**

* Cache variable Unlock

*

* @param string $key cache subscript

* @return Cache_abstract

*/

Abstract public Function unlock ($key);

/**

* Get the cache variable locked

*

* @param string $key cache subscript

* @return BOOL

*/

Abstract public Function islocked ($key);

/**

* Make sure it's not locked

* Up to do $tries sleep waiting to unlock, timeout is 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 times sleep wait for 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;

}

/**

* Buffer Unit Unlock

*

* @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 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 disclosure, cache file format for PHP files, and "" to the beginning

*

* @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 mode

Return Unserialize (@file_get_contents ($cacheFile, False, NULL, 13));

}

return false;

}

/**

* Cache Variables

* To prevent information disclosure, cache file format for PHP files, and "" to the beginning

*

* @param string $key cache variable Subscript

* @param string $value The value of the cached 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 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::d elete ()");

}

$cacheFile = Self::_getcachefile ($key);

if (! @unlink ($cacheFile)) {

throw new Cacheexception ("Cache file could not to 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 ("Could not make cache directory");

}

}

}

To set the access and modification time of a 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.