basit bir query cache sınıfı. php5 gerekiyor evet.
Kullanımı aşağıdaki gibi. elbette class ilk olarak include edilmeli evet. ikinci parametre true olarak verilirse o dosya için bir cache yoksa eğer ilk olarak cache oluşturulcak ve tekrar çağrıldığında cache’deki veri dönecek
kullanımı:
$dbCache = new dbcache(); //eğer query cache'i varsa $retVal = $dbCache->checkQueryCache($query); if($retVal !== FALSE) { return unserialize($retVal); } //bu satır da querycache'ini yazmak için //$return değişkeni query'den dönen sonuçların tutulduğu bir array $dbCache->writeQueryCache($query,serialize($return)); |
bu da sınıf. cache_dir ile tanımlanan klasöre yazma izni verilmeli:
< ?php /* * Class: Simple Query Cache v0.2 * @author: Doruk Özalp * * last update: * 23 / 01 / 2010 23:22 PM * * changelog: * v0.2: * added file modification time support * * v0.1: * initial release * * */ class dbcache { //PRIVATE VARIABLES private $cache_file; private $base_dir; private $this_dir; private $lifetime; private $query_data; private $query; private $cache_dir = "assets/output_cache/"; //CONSTRUCTOR function __construct() { $this->setBaseDir($_SERVER["REQUEST_URI"]); $this->setThisDir($this->cache_dir) . md5($this->getBaseDir())); $this->setLifeTime(3600); // 1-hour } //DECONSTRUCTOR function __destruct() { } //PUBLIC FUNCTIONS public function checkQueryCache($query) { $retVal = FALSE; $this->setQuery($query); $this->setCacheFile($this->getThisDir() . "/" . md5($this->getBaseDir() . $this->getQuery())); if($this->checkCacheFile()) { $retVal = $this->loadCacheFile(); } return $retVal; } public function writeQueryCache($query, $query_data) { $this->setQuery($query); $this->setQueryData($query_data); $this->setCacheFile($this->getThisDir() . "/" . md5($this->getBaseDir() . $this->getQuery())); if($this->checkCacheFile()) { clearstatcache(); if(time() - filemtime($this->getCacheFile()) > $this->getLifeTime()) { $this->writecachefile(); } } else { $this->writecachefile(); } } //PRIVATE FUNCTIONS private function checkCacheFile() { $retVal = FALSE; if(file_exists($this->getCacheFile())) { $retVal = TRUE; } return $retVal; } private function loadCacheFile() { return file_get_contents($this->getCacheFile()); } private function writecachefile() { if(!is_dir($this->getThisDir())) { mkdir($this->getThisDir(), 0777); } $handle = fopen($this->getCacheFile(), 'w+'); fwrite($handle, $this->getQueryData()); fclose($handle); } //SETTER AND GETTERS private function setBaseDir($input) { $this->base_dir = $input; } private function getBaseDir() { return $this->base_dir; } private function setThisDir($input) { $this->this_dir = $input; } private function getThisDir() { return $this->this_dir; } private function setLifeTime($input) { $this->lifetime = $input; } private function getLifeTime() { return $this->lifetime; } private function setCacheFile($input) { $this->cache_file = $input; } private function getCacheFile() { return $this->cache_file; } private function setQueryData($input) { $this->query_data = $input; } private function getQueryData() { return $this->query_data; } private function setQuery($input) { $this->query = $input; } private function getQuery() { return $this->query; } } ?> |