<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of QueryCache
 *
 * @author User
 */
class QueryCache {

	private $query;
	private $cacheQueryName;
	private $cachePathFile;

	public function __construct($queryCacheName,$query)
	{
		if(QUERYCACHE_ENABLE == true)
		{
			$this->cachePathFile = PATH_CORE . "/model/QueryCacheTemp.class.php";
			$this->query = $query;
			$this->cacheQueryName = $queryCacheName;
			$this->Write();
		}
	}

	public function Write()
	{

		// jesli nie ma query w tablicy i nie bylo juz w tym skrypcie dodawane
		if(!isset(QueryCacheTemp::$cacheQuery[$this->cacheQueryName]) && Registry::Get($this->cacheQueryName) != true)
		{
			$fp = fopen($this->cachePathFile,"r+");

			if(count(QueryCacheTemp::$cacheQuery) > 0 || Registry::Get("cacheQueryItems") > 0)
				$cacheFile=",";
			else
				$cacheFile="";
				
			$cacheFile.="
		\"$this->cacheQueryName\" => \"$this->query\");}?>";
			
			if(Registry::Get("cacheQueryItems") > 0)
				fseek($fp, -5,SEEK_END);
			else
				fseek($fp, -5,SEEK_END);
				
			fputs($fp, $cacheFile);

			fclose($fp);
			$item = true;

			if(Registry::Exists("cacheQueryItems"))
				$cacheQueryItems = Registry::Get("cacheQueryItems");
			else
				$cacheQueryItems = 0;

			Registry::Set($this->cacheQueryName, $item);
			Registry::Set("cacheQueryItems", ++$cacheQueryItems);
		}
	}

	public function Clear()
	{
		$fp = fopen($this->cachePathFile,"r+");
		$cacheFile='<?php
class QueryCacheTemp
{
	static $cacheQuery = array(		
	);}?>';
		fputs($fp, $cacheFile);
		fclose($fp);
	}
}
?>