I am writing a caching class with php. It only consumes a lot of RAM. Why did not I solve it like that. I would be glad if you help me about this subject. Where can I make mistakes.
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes) in Config.php on line 65
and I get the error above.
codes are that way.
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;
}
}
You could easily run out of memory if your log file grows large because your class tries to read in and unserialize the entire log file. As your code is currently written, that logFile could grow without bound -- you don't even bother writing any code to rotate the log when it gets large.
You should have told us specifically which line is getting the 'out of memory' error. I'm guessing this is the line that runs out of memory:
$this->cacheLog = unserialize(file_get_contents($this->cacheLogFile));
I'd strongly suggest that you don't ever read in the log file and try to parse it into an array. Instead, you should only ever append to a log. And if you want to avoid filling up your entire hard drive with a giant log file, you should write a proper logging function that will rotate your log files when they get too large.