PHP Class for better cache – fileCache

Last days I’m working making many changes in Open Classifieds and one of them is this new class to handle the cache.

Few days a go I wrote about a cache class and longer a go about application variables for php. This is a mix of both in just one powerful class.

Explanation:

In this class we have 2 different kinds of cache.

First the normal file cache, where we store values in a single file. This is good for example to cahe an entire page.

Second we have an APPLICATION kind integrated in the cache. This means that whatever you store in the APP cache it would be kept in the same file as all the other APP. Really useful to store small amount of data, for example menus, counters etc… Remember that this file is loaded everytime yo create a new object fileCache, you need to be careful to not store many things on it.

Usage:

$cache= new fileCache();
//$cache= new fileCache(180);//second
//$cache= new fileCache(180,'cache/');//seconds and path
 
////cahe application in the same file!!!
$test= $cache->APP("test");
if (!$test){
	$cache->APP("test","value test application cache<br />");
} else echo $test;
$cache->APP("test2","value test 2 application cache<br />"); //same file as variable test
 
//end cache app
 
//normal cache in different files
$test = $cache->cache("test");//getting values from cache
if (!$test) {	//not value from cache found
	$cache->cache("test", "value test normal cache<br />");	//save cache			
}else echo $test;	
//end normal cache
 
$cache->deleteCache(60);//deletes any cache older than X seconds xD

The Class: (Download)

/*
 * Name:	fileCache
 * URL:		http:/neo22s.com/
 * Version:	v0.1
 * Date:	18/12/2009
 * Author:	Chema Garrido
 * Support: http://forum.neo22s.com
 * License: GPL v3
 * Notes:	fileCache class, used in phpMydb
 */
 
/////////////////////class cache
 
class fileCache {
	private $cache_path;//path for the cache
	private $cache_expire;//seconds that the cache expires
	private $application;//application object like in ASP
 	private $application_file;//file for the application object
 
	//cache constructor, optional expiring time and cache path
	public function fileCache($exp_time=3600,$path="cache/"){
		$this->cache_expire=$exp_time;
		$this->cache_path=$path;
		$this->APP_start();//starting application cache with filename...
	}
 
	//returns the filename for the cache
	private function fileName($key){
		return $this->cache_path.md5($key);
	}
 
	//deletes cache from folder
	public function deleteCache($older_than=""){
		if (!is_numeric($older_than)) $older_than=$this->cache_expire;
 
		$files = scandir($this->cache_path); 
		foreach($files as $file){
			if (strlen($file)>2 && time() > (filemtime($this->cache_path.$file) + $older_than) ) {
				unlink($this->cache_path.$file);//echo "<br />-".$file; 
			}
		}
 
	}
 
	//write or read the cache
	public function cache($key, $value=""){
		if ($value!=""){//wants to wirte
			if ($this->get($key)!=$value){//only write if it's different
				$this->put($key, $value);
			}
		}
		else return $this->get($key);//reading
	}
 
	//creates new cache files with the given data, $key== name of the cache, data the info/values to store
	private function put($key, $data){
		$values = serialize($data);
		$filename = $this->fileName($key);
		$file = fopen($filename, 'w');
	    if ($file){//able to create the file
	        fwrite($file, $values);
	        fclose($file);
	    }
	    else return false;
	}
 
	//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
			return false;
		}
		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);
	            return unserialize($data);//return the values
	        }
	        else return false;
		}
		else return false;//was expired you need to create new
 	}
 
 	//load variables from the file
	private function APP_start ($app_file="application"){
		$this->application_file=$app_file;
 
	    if (file_exists($this->cache_path.$this->application_file)){ // if data file exists, load the cached variables
	        $file = fopen($this->cache_path.$this->application_file, "r");// read data file
	        if ($file){
	            $data = fread($file, filesize($this->cache_path.$this->application_file));
	            fclose($file);
	        }
	        // build application variables from data file
	        $this->application = unserialize($data);
	    }
	    else  fopen($this->cache_path.$this->application_file, "w");//if the file does not exist we create it
 
	    //erase the cache every X minutes before loading next time
		$app_time=filemtime($this->cache_path.$this->application_file)+$this->cache_expire;
		if (time()>$app_time) unlink ($this->cache_path.$this->application_file);//erase the cache
	}
 
	// write application data to file
	private function APP_write(){
		    $data = serialize($this->application);
		    $file = fopen($this->cache_path.$this->application_file, "w");
		    if ($file){
		        fwrite($file, $data);
		        fclose($file);
		    }
	}
 
	//returns the value or stores it
	public function APP($var,$value=""){
		if ($value!=""){//wants to wirte
			if ($this->application[md5($var)]!=$value){
				$this->application[md5($var)]=$value;
				$this->APP_write();
			}
		}
		else {//reading
			$return=$this->application[md5($var)];
			if (!isset($return)) return false;//nothing found
			else return $return;//return value
		}
	}
 
}
Related Posts Related Websites
Help sharing and Flatter me ;)

Share

One Comment

  1. [...] cache with the class fileCache: function rssReader($url,$maxItems=15,$cache,$begin="",$end=""){ [...]

Leave a Reply

Follow me