PHP 在关联数组中使用长文本有一些影响吗?

发布于 2024-08-02 08:12:01 字数 1210 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何时共饮酒 2024-08-09 08:12:02

在深入实施之前,您应该知道 mysql它有自己的查询缓存,并且它比您的实现有几个主要优点:

  • 缓存在所有 PHP 请求之间共享。
  • 当表数据发生变化时,缓存的结果会自动清理。
  • 缓存限制在一定的内存大小(很少使用的查询将从缓存中删除)

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:

  • The cache is shared among all PHP requests.
  • Cached results are automatically cleaned up when the table data changes.
  • The cache is limited to a certain memory size (rarely-used queries will be dropped out of the cache)
亣腦蒛氧 2024-08-09 08:12:02

缓存 mysql 数据可能有点冒险,它对事情做了很多假设,但是话虽如此,sql 字符串的 md5 校验和是为缓存数据生成 id 的好方法,但是您需要规范化例如,首先检查 sql:

'select 1+2'
'select 1 + 2'
' select 1 +2'

每个查询都会给出不同的校验和,您需要确保考虑到同一查询之间的任何细微差异。

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:

'select 1+2'
'select 1 + 2'
' select 1 +2'

each one will give a different checksum, you'll need to make sure any little differences between the same query are taken care of.

北方。的韩爷 2024-08-09 08:12:02

除了一点额外的内存使用之外,应该不会有太大的影响,但与查询的结果相比,我认为这不会成为问题。

关联数组是有序映射,因此查找正确的索引不应受到字符串总长度的太大影响。唯一的缺点是大多数字符串都以完全相同的文本开头。

我不会仅依赖 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.

溺渁∝ 2024-08-09 08:12:02

哈希值(md5、sha1)会占用更少的内存空间。

A hash (md5, sha1) will eat less memory space.

方圜几里 2024-08-09 08:12:02

如果查询很长,我会选择使用某种哈希(如 md5):

  • 它可能会使用更少的内存(我想不是那么相关),
  • 它允许您将其存储在其他类型的存储中缓存,例如文件或 APC 或 memcached 或其他任何内容,而无需更改索引。

考虑到与 md5 发生冲突的风险较低,这样做似乎并不“危险”。

我看到的唯一问题是,在调试时,无论是使用 var_dump 或任何类型的“真正的”调试器,您可能会更难找到缓存的数据:-(

If the query is quite long, I'd go for using some kind of hash (like md5) :

  • it will probably use a bit less memory (not that relevant, I suppose)
  • it will allow you to store that in some other kind of cache, like file or APC or memcached or whatever without having to change the index.

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 :-(

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文