从opencart里面抠出来一个数据缓存类,看着感觉不错
2007-08-14 13:53
387 查看
class cache {
var $maxlifetime = 3600;
function cache() {
if (!is_writable(DIR_CACHE)) {
exit('Error: Could not write to cache directory!');
}
foreach (glob(DIR_CACHE . 'cache.*') as $file) {
$array = explode('.', end(explode('/', $file)));
if ($array[2] < time()) {
$this->delete($array[1]);
}
}
}
function set($key, $value) {
$this->delete($key);
$file = fopen(DIR_CACHE . 'cache.' . $key . '.' . (time() + $this->maxlifetime), 'a');
fwrite($file, serialize($value));
fclose($file);
}
function get($key) {
foreach (glob(DIR_CACHE . 'cache.' . $key . '.*') as $cache) {
$contents = fopen($cache, 'r');
$result = fread($contents, filesize($cache));
fclose($contents);
return unserialize($result);
}
}
function delete($key) {
foreach (glob(DIR_CACHE . 'cache.' . $key . '.*') as $file) {
unlink($file);
}
}
}
var $maxlifetime = 3600;
function cache() {
if (!is_writable(DIR_CACHE)) {
exit('Error: Could not write to cache directory!');
}
foreach (glob(DIR_CACHE . 'cache.*') as $file) {
$array = explode('.', end(explode('/', $file)));
if ($array[2] < time()) {
$this->delete($array[1]);
}
}
}
function set($key, $value) {
$this->delete($key);
$file = fopen(DIR_CACHE . 'cache.' . $key . '.' . (time() + $this->maxlifetime), 'a');
fwrite($file, serialize($value));
fclose($file);
}
function get($key) {
foreach (glob(DIR_CACHE . 'cache.' . $key . '.*') as $cache) {
$contents = fopen($cache, 'r');
$result = fread($contents, filesize($cache));
fclose($contents);
return unserialize($result);
}
}
function delete($key) {
foreach (glob(DIR_CACHE . 'cache.' . $key . '.*') as $file) {
unlink($file);
}
}
}
相关文章推荐
- C中打印二进制位数的一个例子----当初学习5的反码打印出来是-6的一个例子(感觉不错的)
- 民间秘方,里面的方子都是一个老中医几十年的心血哦。(感觉不错就摘过来了)
- Monty Hall Problem的一个图解,感觉不错
- 感觉不错的一个帖子
- 下载了一个IBM Rational RequisitePro Trial,感觉IBM的用户体验不错
- 推荐一个学习网页知识的网站,感觉不错哦
- 一个数组arr,长度为N,N小于10^8,里面数字的范围是1~N,是否有重复,并统计出来
- 推荐一个站点:里面有很多不错的长文
- 在oracle里面为读取出来的数据添加一个序号
- 每天一个python小程序 007有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
- 最近搞了一个免费空间,感觉不错!
- 题目:有一个桶,里面有白球、黑球各100个,人们必须按照以下的规则把球取出来:
- 刚刚用CSS写了一个按钮,感觉不错,拿出来显摆一下...
- 一个字符串有两个A,计算里面的字符个数,并打印出来
- 正在读王小波的《沉默的大多数》感觉不错的话就贴出来
- 发现一个在线jpg png转ICO的网站,用着感觉不错
- kingate代理软件--一个不错的开源代理(不过里面代码好乱),win32调试日记
- 今天发现一个自动化测试的群感觉不错
- 推荐一个站点:里面有很多不错的长文