PHP缓存高使用ram

问题描述 投票:1回答:1

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

致命错误:第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;
        }
    }

}

config.php文件

class Config {
    public static function Get($string = '') {
        global $Globals;
        if($string) {

            $config = $Globals['Config'];
            $string = explode('/', $string); // Error row.

            foreach ($string as $path) {
                if(isset($config[$path])) {
                    $config = $config[$path];
                }
            }
            return $config;
        }
        return false;
    }
}
php caching
1个回答
1
投票

如果日志文件变大,您可能很容易耗尽内存,因为您的类试图读入并反序列化整个日志文件。正如您的代码当前编写的那样,该logFile可以无限制地增长 - 您甚至无需编写任何代码来在日志变大时轮换日志。

您应该具体告诉我们哪条线路正在出现“内存不足”错误。我猜这是内存耗尽的行:

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

我强烈建议您不要在日志文件中读取并尝试将其解析为数组。相反,您应该只追加日志。如果你想避免用一个巨大的日志文件填满你的整个硬盘驱动器,你应该编写一个适当的日志记录功能,当它们变得太大时将轮换你的日志文件。

© www.soinside.com 2019 - 2024. All rights reserved.