一个强大的PHP缓存类(8)
//end overloads
//////////Cache for files individually///////////////////
//creates new cache files with the given data, $key== name of the cache, data the info/values to store
private function put($key, $data){
if ( $this->get($key)!= $data ){//only write if it's different
$values = serialize($data);
$filename = $this->fileName($key);
$file = fopen($filename, 'w');
if ($file){//able to create the file
$this->addLog('writting key: '.$key.' file: '.$filename);
fwrite($file, $values);
fclose($file);
}
else $this->addLog('unable to write key: '.$key.' file: '.$filename);
}//end if different
}
//returns cache for the given key
private function get($key){
$filename = $this->fileName($key);
if (!file_exists($filename) || !is_readable($filename)){//can't read the cache
$this->addLog('can\'t read key: '.$key.' file: '.$filename);
return null;
}
if ( time() < (filemtime($filename) + $this->cache_expire) ) {//cache for the key not expired
$file = fopen($filename, 'r');// read data file
if ($file){//able to open the file
$data = fread($file, filesize($filename));
fclose($file);
$this->addLog('reading key: '.$key.' file: '.$filename);
return unserialize($data);//return the values
}
else{
$this->addLog('unable to read key: '.$key.' file: '.$filename);
return null;
}
}
else{
$this->addLog('expired key: '.$key.' file: '.$filename);
unlink($filename);
顶(0)
踩(0)
- 最新评论
