<?php

class DbCache {
		private $query;
		private $cacheTime = 30;
		private $cacheFile;
		private $cachePath = DBCACHE_PATH;
		private $cacheEnable = DBCACHE_ENABLE;
	
	public function __construct($query, $time=null) {
		$this->query = $query;
		$this->cacheFile = md5($query);
		if($time!=null) $this->cacheTime = $time;
		
	}
	
	public function CacheCheck() {
		if($this->cacheEnable == false || ($this->cacheEnable == true && is_file($this->cachePath.$this->cacheFile) && filemtime($this->cachePath.$this->cacheFile) > (time() - $this->cacheTime))) {
			return true;
		} else {
			return false;
		}
	}
	
	public function CacheWrite($result) {
		if($this->cacheEnable != false) {
		$records = serialize($result);
		$fp = fopen($this->cachePath.$this->cacheFile, "w");
		fputs($fp, $records);
		fclose($fp);
		}
	
	}
	
	public function CacheRead() {
		$records = unserialize(file_get_contents($this->cachePath.$this->cacheFile));
		return $records; 
	}
	
	
}

?>