PHP 在关联数组中使用长文本有一些影响吗?
我在 PHP 中做一个数据库类,我想在关联数组中缓存查询结果,我的想法是使用 sql 语句作为缓存数组的索引,这可能是一个好主意吗?或者我应该使用sql中的md5?
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];
}
}
再会
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在深入实施之前,您应该知道 mysql它有自己的查询缓存,并且它比您的实现有几个主要优点:
Before you get too far through your implementation, you should know that mysql does its own query caching and it has several major advantages over your implementation:
缓存 mysql 数据可能有点冒险,它对事情做了很多假设,但是话虽如此,sql 字符串的 md5 校验和是为缓存数据生成 id 的好方法,但是您需要规范化例如,首先检查 sql:
每个查询都会给出不同的校验和,您需要确保考虑到同一查询之间的任何细微差异。
caching mysql data could be a little risky, it makes a lot of assumptions about things, however having said that a md5 checksum of the sql string is an good way of generating a id for your cache data, however you'll need to normalise the sql first, for example:
each one will give a different checksum, you'll need to make sure any little differences between the same query are taken care of.
除了一点额外的内存使用之外,应该不会有太大的影响,但与查询的结果相比,我认为这不会成为问题。
关联数组是有序映射,因此查找正确的索引不应受到字符串总长度的太大影响。唯一的缺点是大多数字符串都以完全相同的文本开头。
我不会仅依赖 MD5 哈希来进行查询。有可能(如果不太可能)发生哈希冲突,并且它会选择完全不同的查询结果。然而,对于您的应用程序来说,这可能是可接受的风险。
就我个人而言,我根本不会在查询级别执行此操作。可缓存性通常取决于返回数据的类型。如果您在此级别进行缓存,您的应用程序将完全不知道它正在缓存什么。
There shouldn't be a large impact, apart from a little extra memory usage, but compared to the result of the query, I don't think it will be a problem.
Associative arrays are ordered maps, so finding the right index shouldn't be affected too much by the total length of the string. The only downside would be that most strings start with the exact same text.
I wouldn't rely on just an MD5 hash for your queries. It's possible (if unlikely) that a hash collision would occur, and it would select a completely different query result. It might be an acceptable risk for your application however.
Personally, I wouldn't do this at the query level at all. The cacheability usually depends on the type of data returned. If you cache at this level, your application would be completely agnostic of what it is caching.
哈希值(md5、sha1)会占用更少的内存空间。
A hash (md5, sha1) will eat less memory space.
如果查询很长,我会选择使用某种哈希(如 md5):
考虑到与 md5 发生冲突的风险较低,这样做似乎并不“危险”。
我看到的唯一问题是,在调试时,无论是使用 var_dump 或任何类型的“真正的”调试器,您可能会更难找到缓存的数据:-(
If the query is quite long, I'd go for using some kind of hash (like md5) :
Considering the low risks of collisions with md5, it doesn't seem "dangerous" to do that.
The only problem I see is it might get harder for you to find your cached data when debugging, be it with
var_dump
or any kind of "real" debugger :-(