Soul_Stealler
Registered
Код:
<?php
/*
Title: MySQL Cache
Created by: Radoslav Mazganov
Email: raadoo at abv dot bg
Portfolio: http://soul-design.org/
How To Use
$cache->ClearCache('7c1141fbce2eb2407964b25b59b43ed4');
$cache->ClearCache();
$cache->Query("SELECT * FROM `table`");
$cache->Query("SELECT * FROM `table`", true);
*/
class MySQLCache{
public $dir = 'cache';
public $ext = 'txt';
function QueryCached($key){
return file_exists($this->dir.'/'.$key.'.'.$this->ext);
}
function ReturnQuery($key){
$file = $this->dir.'/'.$key.'.'.$this->ext;
$handle = fopen($file, "r");
$contents = fread($handle, filesize($file));
fclose($handle);
return unserialize(trim($contents));
}
function CacheQuery($query, $key, $result = false){
$results = mysql_query($query);
while ($row = mysql_fetch_array($results)) $all_rows[] = $row;
$temp = $all_rows;
$all_rows = serialize($all_rows);
$fp = fopen($this->dir.'/'.$key.'.'.$this->ext, 'w');
fwrite($fp, $all_rows);
fclose($fp);
if($result) return $temp;
}
function ClearCache($key = false){
if(strlen($key) == '32'){
if(self::QueryCached($key)) unlink($this->dir.'/'.$key.'.'.$this->ext);
} else {
$all_files = scandir($this->dir.'/');
foreach($all_files as $file){
if(end(explode('.', $file)) == $this->ext) unlink($this->dir.'/'.$file);
}
}
}
function Query($query, $result = false){
$key = md5($query);
if($result){
if(self::QueryCached($key)){
$this->result = self::ReturnQuery($key);
} else $this->result = self::CacheQuery($query, $key, true);
} else {
if(!self::QueryCached($key)) self::CacheQuery($query, $key);
}
}
}
?>
Моля не разпространявайте по форуми и сайтове :wink: