$cache = new Filecache ();
$new _arr = $cache->get (' Gsmcache ');//yourkey is the cache name that you define for each data that you want to cache
if ($new _arr===false) {
$new _arr= "Data returned by the database";
$cache->set (' Gsmcache ', $new _arr,3600);/cache 3,600 seconds
}
<?php
/**
* File Cache class
*
* @copyright blog.itiwin.cn
* @author More
* @package Cache
* @version v0.1
*/
Class Filecache {
/**
* @var string $cachePath cache file directory
* @access Public
*/
Public $cachePath = './';
/**
* Constructor
* @param string $path cache file directory
*/
function __construct ($path = NULL) {
if ($path) {
$this->cachepath = $path;
}
}
/**
* destructor
*/
function __destruct () {
Nothing
}
/**
* Set the value of the key to $key in the cache, and if the entry does not exist, create a new item
* @param string $key key value
* @param mix $var value
* @param int $expire expiration number of seconds
* @param int $flag logo bit
* @return BOOL Returns TRUE if successful, and returns FALSE if it fails.
* @access Public
*/
Public function set ($key, $var, $expire = 36000, $flag = 0) {
$value = serialize ($var);
$timeout = time () + $expire;
$result = safe_file_put_contents ($this->cachepath. UrlEncode ($key). Cache ',
$timeout. ' <<%-==-%>> '. $value);
return $result;
}
/**
* Get the value of the item with key $key in cache
* @param string $key key value
* @return String returns False if the item does not exist
* @access Public
*/
Public function Get ($key) {
$file = $this->cachepath. UrlEncode ($key). Cache ';
if (file_exists ($file)) {
$content = Safe_file_get_contents ($file);
if ($content ===false) {
return false;
}
$tmp = Explode (' <<%-==-%>> ', $content);
$timeout = $tmp [0];
$value = $tmp [1];
if (Time () > $timeout) {
$this->delete ($key);//delete file expired
$result = false;
} else {
$result = Unserialize ($value);
}
} else {
$result = false;
}
return $result;
}
/**
* Empty all items in cache
* @return returns TRUE if successful and FALSE if failed.
* @access Public
*/
Public Function flush () {
$fileList = Filesystem::ls ($this->cachepath,array (), ' ASC ', true);
Return Filesystem::rm ($fileList);
}
/**
* Delete the value of the key in cache that is $key
* @param string $key key value
* @return returns TRUE if successful and FALSE if failed.
* @access Public
*/
Public Function Delete ($key) {
Return Filesystem::rm ($this->cachepath. $key. Cache ");
}
}
if (!function_exists (' safe_file_put_contents ')) {
function safe_file_put_contents ($filename, $content)
{
$fp = fopen ($filename, ' WB ');
if ($fp) {
Flock ($FP, LOCK_EX);
Fwrite ($fp, $content);
Flock ($FP, lock_un);
Fclose ($FP);
return true;
} else {
return false;
}
}
}
if (!function_exists (' safe_file_get_contents ')) {
function Safe_file_get_contents ($filename)
{
$fp = fopen ($filename, ' RB ');
if ($fp) {
Flock ($FP, lock_sh);
Clearstatcache ();
$filesize = FileSize ($filename);
if ($filesize > 0) {
$data = Fread ($fp, $filesize);
}
Flock ($FP, lock_un);
Fclose ($FP);
return $data;
} else {
return false;
}
}
}