Im doing a database class in PHP and I want to make cache of the result of the querys in a associative array, My idea is to use the sql statment as the index of the cache array, its could be a good idea? or should I use a md5 from the sql?
class DB{
const HOST = 'localhost'; //Your Database Host!
const USER = 'user'; //Your Database Username!
const PASSWORD = 'pass'; //Your Database Password!
const DATABASE = 'database'; //Your Database Name!
private static $Instance;
private static $cache = array();
private function __construct(){
self::$Instance = mysql_connect(self::HOST, self::USER, self::PASSWORD) or die("Could not connect to database server<br/><b>Error:</b>".mysql_error());
mysql_select_db(self::DATABASE) or die("Could not connect to database<br/><b>Error:</b>".mysql_error());
return self::$Instance;
}
public static function DB(){
if(!isset(self::$Instance)){
$c = __CLASS__;
new $c();
}
return self::$Instance;
}
public static function QueryUnique($query){
$query = "$query LIMIT 1";
//$h = md5($query);
$h = $query;
if(isset(self::$cache[$h]))return self::$cache[$h];
$result = mysql_query($query, self::DB());
self::$cache[$h] = mysql_fetch_array($result);
return self::$cache[$h];
}
}
Good Day