<?php
class Cache {
var $status = True; // 值为True表示开启缓存;值False表示关闭缓存功能。
var $cacheDir = 'cache/'; //存放缓存文件的默认目录
var $cacheFile = ''; //缓存文件的真实文件名
var $timeOut = 1224000000; // 内容被重复使用的最长时限
var $startTime = 0; // 程序执行的开始时间
var $caching = true; // 是否需要对内容进行缓存;值为False表示直接读取缓存文件内容 function startCache(){
$this->startTime = $this->getMicroTime();
ob_start();
if ($this->status){
$this->cacheFile = $this->cacheDir . urlencode( $_SERVER['REQUEST_URI'] );
if ( (file_exists($this->cacheFile)) &&
((fileatime($this->cacheFile) + $this->timeOut) > time()) )
{
//从缓存文件中读取内容
$handle = fopen($this->cacheFile , "r");
$html = fread($handle,filesize($this->cacheFile));
fclose($handle); // 显示内容
echo $html; // 显示程序执行时间
//echo '<p>Total time: '
//.round(($this->getMicroTime())-($this->startTime),4).'</p>'; //退出程序
exit();
}
else
{
//置缓存标志caching为true
$this->caching = true; }
}
} function endCache(){
if ($this->status){
if ( $this->caching )
{ //首先输出页面内容,然后将输出内容保存在缓存文件中
$html = ob_get_clean();
echo $html;
$handle = fopen($this->cacheFile, 'w' );
fwrite ($handle, $html );
fclose ($handle); //显示页面执行时间
//echo '<p>Total time: '.round(($this->getMicroTime()-$this->startTime),4).'</p>';
}
}
} function cleanCache(){
if ($handle = opendir($this->cacheDir)) {
while (false !== ($file = readdir($handle))) {
if (is_file($this->cacheDir.$file)) unlink($this->cacheDir.$file);
} closedir($handle);
}
}
function getMicroTime() {
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
} }
?>
这个缓存类 只能在特定的缓存目录下 存文件,如果一个文件夹下 有上万个页面那打开速度 肯定很慢,我现在想的是 5000个文件 一个文件夹 cache 是总目录 然后在里面自动建立二级目录 二级目录里面存缓存文件。 就这个要求! 先谢谢各位啦!
class Cache {
var $status = True; // 值为True表示开启缓存;值False表示关闭缓存功能。
var $cacheDir = 'cache/'; //存放缓存文件的默认目录
var $cacheFile = ''; //缓存文件的真实文件名
var $timeOut = 1224000000; // 内容被重复使用的最长时限
var $startTime = 0; // 程序执行的开始时间
var $caching = true; // 是否需要对内容进行缓存;值为False表示直接读取缓存文件内容 function startCache(){
$this->startTime = $this->getMicroTime();
ob_start();
if ($this->status){
$this->cacheFile = $this->cacheDir . urlencode( $_SERVER['REQUEST_URI'] );
if ( (file_exists($this->cacheFile)) &&
((fileatime($this->cacheFile) + $this->timeOut) > time()) )
{
//从缓存文件中读取内容
$handle = fopen($this->cacheFile , "r");
$html = fread($handle,filesize($this->cacheFile));
fclose($handle); // 显示内容
echo $html; // 显示程序执行时间
//echo '<p>Total time: '
//.round(($this->getMicroTime())-($this->startTime),4).'</p>'; //退出程序
exit();
}
else
{
//置缓存标志caching为true
$this->caching = true; }
}
} function endCache(){
if ($this->status){
if ( $this->caching )
{ //首先输出页面内容,然后将输出内容保存在缓存文件中
$html = ob_get_clean();
echo $html;
$handle = fopen($this->cacheFile, 'w' );
fwrite ($handle, $html );
fclose ($handle); //显示页面执行时间
//echo '<p>Total time: '.round(($this->getMicroTime()-$this->startTime),4).'</p>';
}
}
} function cleanCache(){
if ($handle = opendir($this->cacheDir)) {
while (false !== ($file = readdir($handle))) {
if (is_file($this->cacheDir.$file)) unlink($this->cacheDir.$file);
} closedir($handle);
}
}
function getMicroTime() {
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
} }
?>
这个缓存类 只能在特定的缓存目录下 存文件,如果一个文件夹下 有上万个页面那打开速度 肯定很慢,我现在想的是 5000个文件 一个文件夹 cache 是总目录 然后在里面自动建立二级目录 二级目录里面存缓存文件。 就这个要求! 先谢谢各位啦!
解决方案 »
- 写下这篇文章时,我的心情很沉重
- 关于php中使用ecshop问题
- 请各位高手提供下级联菜单和mysql一起使用的源码。
- linux下用php创建文件夹的问题
- php 利用openoffice把office转成pdf格式
- dedecms如何创建地图
- js 判读输入时间不能小于今天时间
- redhat下安装yum遇到问题
- CentOS release 6.3 安装lnmp集成包IE无法获取session值,其他浏览器可以,为什么?
- 1.4.3版本的UEditor可以直接上传视频容量大于6MB到远程服务器上,怎么显示“上传失败”
- 打开php页面为何速度很慢?
- php dio_read()一个小小问题.大家来讨论一下
有必要做的这么强大吗?