帮我 在 php缓存类 增加个自动增加目录的功能

<?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 是总目录 然后在里面自动建立二级目录 二级目录里面存缓存文件。 就这个要求! 如果能解决 我把我的分 在送1000 !

原理就是把$_SERVER['REQUEST_URI']经过md5处理后得到的字符串(2510c39011c5be704182423e3a695e91)前4个字符作为目录,即:25/10。
每个$_SERVER['REQUEST_URI']都对应随即散列的md5值,即可平均到每个目录中。
每层目录的最大个数就是(26+10)*(26+10)=1296,总的文件就是1296*1296=1679616,在合理的范围之内。

只是不知道1000分怎么才能送啊,呵呵,开玩笑了
<?php

class Cache {
var $status = True; // 值为True表示开启缓存;值False表示关闭缓存功能。
var $cacheDir = 'cache/'; //存放缓存文件的默认目录
var $cacheFile = ''; //缓存文件的真实文件名
var $timeOut = 1224000000; // 内容被重复使用的最长时限
var $startTime = 0; // 程序执行的开始时间
var $caching = true; // 是否需要对内容进行缓存;值为False表示直接读取缓存文件内容

function Cache(){
$depth = 2; //分两层目录
$code = md5($_SERVER['REQUEST_URI']);
for($i=0, $j=0; $j<$depth; $j++,$i+=2){
$this->cacheDir .= $code{$i} . $code{$i+1} . '/';
if(!file_exists($dir)){
@mkdir($this->cacheDir, '0777');
}
}
}
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);
}

}
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网