php缓存类(php5版)
PHP:
<?php
/**
* php5写的缓存类,用于更新缓存
*
*
* To contact the author write to {@link mailto:meng.chao@163.com}
* or visit author's website http://www.eb163.com
*
* @author 小超
* @version $Id: cache.class.php,v 1.3 2008/2/17 09:30:00 mengchao Exp $
* @package system
*/
class CacheHandler
{
private static $cache_dir;// 缓存目录
/**
* 构造函数
*
*/
function __construct()
{
self::$cache_dir = "./cache";
}
/**
* 设置缓存目录
*
* @param string $cache_dir 缓存目录
*/
public static function setCacheDir($cache_dir)
{
self::$cache_dir = $cache_dir;
}
/**
* 取得所有缓存
*
* @param string $type 缓存名称
*/
public static function getCache($type)
{
$file_path = self::$cache_dir.'/'.$type.".cache.php";
if(!file_exists($file_path))
{
Return false;
}
require_once($file_path);
return $_CACHE[$type];
}
/**
* 设置缓存
*
* @param string $type 缓存名称
* @param array $data 缓存数据
*/
public static function setCache($type, $data)
{
$file_path = self::$cache_dir.'/'.$type.".cache.php";
$str = var_export($data,true);
$out = "<?php\r\n";
$out .= "//cachetime ".date("Y-m-d H:i:s")."\r\n";
$out .= "\$_CACHE['$type'] = ".$str.";\r\n";
$out .= "?>";
$fp = fopen($file_path, 'w');
fwrite($fp, $out);
fclose($fp);
return $file_path;
}
}
?>
