PHP高速缓存高使用ram

PHP高速缓存高使用ram,php,caching,Php,Caching,我正在用php编写一个缓存类。它只消耗大量内存。我为什么不这样解决呢。如果你能在这个问题上帮助我,我会很高兴的。我在哪里会犯错 致命错误:第65行Config.php中允许的内存大小134217728字节已用完(尝试分配24字节) 我得到了上面的错误 代码是那样的 class Cache { var $doNotCache = array("admin","profile"); var $cacheDir = NULL; var $cacheTime = 21600;

我正在用php编写一个缓存类。它只消耗大量内存。我为什么不这样解决呢。如果你能在这个问题上帮助我,我会很高兴的。我在哪里会犯错

致命错误:第65行Config.php中允许的内存大小134217728字节已用完(尝试分配24字节)

我得到了上面的错误

代码是那样的

class Cache {

    var $doNotCache = array("admin","profile");

    var $cacheDir = NULL;
    var $cacheTime = 21600;
    var $caching = false;
    var $cacheFile;
    var $cacheFileName;
    var $cacheLogFile;
    var $cacheLog;

    function __construct(){
        $this -> cacheDir = CACHE_DIR;
        $this -> cacheFile = md5($_SERVER['REQUEST_URI']);
        $this -> cacheFileName = $this -> cacheDir.'/'.$this-> cacheFile. '.cache';
        $this -> cacheLogFile = $this -> cacheDir."/log.txt";
        if(!is_dir( $this-> cacheDir )) mkdir( $this-> cacheDir, 0755);
        if(file_exists($this->cacheLogFile))
            $this->cacheLog = unserialize(file_get_contents($this->cacheLogFile));
        else
            $this->cacheLog = array();

    }

    function start(){
        $location = array_slice(explode('/',$_SERVER['REQUEST_URI']), 2);
        if(!in_array($location[0],$this->doNotCache)){
            if(file_exists($this->cacheFileName) && (time() - filemtime($this->cacheFileName)) < $this->cacheTime && $this->cacheLog[$this->cacheFile] == 1){
                $this->caching = false;
                echo file_get_contents($this->cacheFileName);
                exit();
            }else{
                $this->caching = true;
                ob_start();
            }
        }
    }

    function end(){
        if($this->caching){
            file_put_contents($this->cacheFileName,ob_get_contents());
            ob_end_flush();
            $this->cacheLog[$this->cacheFile] = 1;
            if(file_put_contents($this->cacheLogFile,serialize($this->cacheLog)))
                return true;
        }
    }

    function purge($location){
        $location = base64_encode($location);
        $this->cacheLog[$location] = 0;
        if(file_put_contents($this->cacheLogFile,serialize($this->cacheLog)))
            return true;
        else
            return false;
    }

    function purge_all(){
        if(file_exists($this->cacheLogFile)){
            foreach($this->cacheLog as $key=>$value) $this->cacheLog[$key] = 0;
            if(file_put_contents($this->cacheLogFile,serialize($this->cacheLog)))
                return true;
            else
                return false;
        }
    }

}

如果您的日志文件变大,因为您的类试图读入并取消序列化整个日志文件,那么很容易耗尽内存。由于您的代码是当前编写的,因此日志文件可以不受限制地增长——您甚至不必编写任何代码来在日志变大时旋转日志

您应该明确告诉我们哪一行出现“内存不足”错误。我猜这是内存不足的一行:

$this->cacheLog = unserialize(file_get_contents($this->cacheLogFile));

我强烈建议您不要读取日志文件并尝试将其解析为数组。相反,您应该只附加到日志。如果你不想用一个巨大的日志文件填满整个硬盘,你应该编写一个适当的日志函数,当日志文件太大时,它会旋转日志文件。

为什么你要尝试取消日志文件的序列化?我加入了另一个变量。你可能是对的,但是我在没有任何数据的日志文件中得到了这个错误。@APAWebBilişimHizmetleri您能确定是哪行代码导致了“内存不足”错误吗?PHP应该告诉您它是哪一行,您可以与我们共享。第65行的Config.php。我更新了我的代码。你发布的config.php代码没有65行。你需要再努力一点。例如,发布所有相关代码并识别导致错误的行。我删除了描述行。第65行:$string=explode(“/”,$string);
$this->cacheLog = unserialize(file_get_contents($this->cacheLogFile));