简单实用的cache类
时间:2009-11-23
来源:互联网
[code]<?php
class Cache extends Base {
public $cacheDir = '/cache';
public $lifeTime = 3600;
public $dirLevels = 0; //缓存保存时的目录层数
private $md5id; //md5后的缓存id
const MAX_CACHE_DIR_LEVELS = 16; //最大缓存目录层次
public function __construct($config=array())
{
foreach ($config as $key=>$val) {
$this->$key=$val;
}
if ($this->dirLevels>self::MAX_CACHE_DIR_LEVELS) {
$this->dirLevels=self::MAX_CACHE_DIR_LEVELS;
}
}
/**
* 读取缓存
*
* @param string $id
* @return mixed|boolean
*/
public function get($id)
{
$this->md5id=md5($id);
$file=$this->getCacheFile($this->md5id);
if (is_file($file) && filemtime($file)>time()) {
return unserialize(file_get_contents($file));
} else {
return false;
}
}
/**
* 保存缓存
*
* @param mixed $data
* @param string $id
*/
public function save($data,$lifeTime=null,$id=null)
{
$lifeTime=time() + (strlen($lifeTime) ? $lifeTime : $this->lifeTime);
$md5id=strlen($id) ? md5($id) : $this->md5id;
$dir=$this->cacheDir.$this->getDirLevel($md5id);
if (!file_exists($dir)) {
mkdir($dir,0777,true);
}
$cacheFile=$dir.'/cache_'.$md5id;
file_put_contents($cacheFile,serialize($data),LOCK_EX);
touch($cacheFile,$lifeTime);
}
/**
* 获取缓存文件名
*
*/
private function getCacheFile($md5id)
{
return $this->cacheDir.$this->getDirLevel($md5id).'/cache_'.$md5id;
}
/**
* 获取缓存目录层次
*
*/
private function getDirLevel($md5id)
{
$levels=array();
$levelLen=2;
for ($i=0; $i<$this->dirLevels; $i++) {
$levels[]='cache_'.substr($md5id,$i*$levelLen,$levelLen);
}
return !count($levels) ? '' : '/'.implode('/',$levels);
}
}//class[/code]
class Cache extends Base {
public $cacheDir = '/cache';
public $lifeTime = 3600;
public $dirLevels = 0; //缓存保存时的目录层数
private $md5id; //md5后的缓存id
const MAX_CACHE_DIR_LEVELS = 16; //最大缓存目录层次
public function __construct($config=array())
{
foreach ($config as $key=>$val) {
$this->$key=$val;
}
if ($this->dirLevels>self::MAX_CACHE_DIR_LEVELS) {
$this->dirLevels=self::MAX_CACHE_DIR_LEVELS;
}
}
/**
* 读取缓存
*
* @param string $id
* @return mixed|boolean
*/
public function get($id)
{
$this->md5id=md5($id);
$file=$this->getCacheFile($this->md5id);
if (is_file($file) && filemtime($file)>time()) {
return unserialize(file_get_contents($file));
} else {
return false;
}
}
/**
* 保存缓存
*
* @param mixed $data
* @param string $id
*/
public function save($data,$lifeTime=null,$id=null)
{
$lifeTime=time() + (strlen($lifeTime) ? $lifeTime : $this->lifeTime);
$md5id=strlen($id) ? md5($id) : $this->md5id;
$dir=$this->cacheDir.$this->getDirLevel($md5id);
if (!file_exists($dir)) {
mkdir($dir,0777,true);
}
$cacheFile=$dir.'/cache_'.$md5id;
file_put_contents($cacheFile,serialize($data),LOCK_EX);
touch($cacheFile,$lifeTime);
}
/**
* 获取缓存文件名
*
*/
private function getCacheFile($md5id)
{
return $this->cacheDir.$this->getDirLevel($md5id).'/cache_'.$md5id;
}
/**
* 获取缓存目录层次
*
*/
private function getDirLevel($md5id)
{
$levels=array();
$levelLen=2;
for ($i=0; $i<$this->dirLevels; $i++) {
$levels[]='cache_'.substr($md5id,$i*$levelLen,$levelLen);
}
return !count($levels) ? '' : '/'.implode('/',$levels);
}
}//class[/code]

pietty0327.exe (489.96 KB)
作者: 无论在何处 发布时间: 2009-11-23
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28