简体   繁体   English

如何使用Memcache缓存mysql_query?

[英]How to cache a mysql_query using memcache?

I would like to know if it's possible to store a "ressource" within memcache, I'm currently trying the following code but apparently it's not correct: 我想知道是否可以在内存缓存中存储“资源”,我目前正在尝试以下代码,但显然不正确:

$result = mysql_query($sSQL);
$memcache->set($key, $result, 0, $ttl);
return $result;

I have to disagree with zerkms. 我必须不同意策尔克斯。 Just because MySQL has a caching system (actually, it has several), doesn't mean that there's no benefit to optimizing your database access. 仅仅因为MySQL有一个缓存系统(实际上有多个),并不意味着优化数据库访问没有任何好处。 MySQL's Query Cache is great, but it still has limitations: MySQL的查询缓存很棒,但是仍然有局限性:

  • it's not suitable for large data sets 它不适用于大型数据集
  • queries have to be identical (character for character) 查询必须相同(字符字符)
  • it does not support prepared statements or queries using user-defined functions, temporary tables, or tables with column-level privileges 它不支持使用用户定义的函数,临时表或具有列级特权的表的预准备语句或查询
  • cache results are cleared every time the table is modified, regardless of whether the result set is affected 每次修改表时都会清除缓存结果,无论结果集是否受到影响
  • unless it resides on the same machine as the web server it still incurs unnecessary network overhead 除非它与Web服务器位于同一台计算机上,否则仍会导致不必要的网络开销

Even with a remote server, Memcached is roughly 23% faster than MQC . 即使使用远程服务器, Memcached也比MQC快23% And using APC's object cache, you can get up to a 990% improvement over using MQC alone. 而且,使用APC的对象缓存,与单独使用MQC相比,您最多可以提高990%。

So there are plenty of reasons to cache database result sets outside of MySQL's Query Cache. 因此,有很多理由将数据库结果集缓存在MySQL的查询缓存之外。 After all, you cache result data locally in a PHP variable when you need to access it multiple times in the same script. 毕竟,当您需要在同一脚本中多次访问结果数据时,会将结果数据本地缓存在PHP变量中。 So why wouldn't you extend this across multiple requests if the result set doesn't change? 那么,如果结果集不变,为什么不将其扩展到多个请求呢?

And just because the server is fast enough doesn't mean you shouldn't strive to write efficient code. 而且,仅仅因为服务器足够快并不意味着您不应该努力编写高效的代码。 It's not like it takes that much effort to cache database results—especially when accelerators like APC and Memcached were designed for this exact purpose. 缓存数据库结果并不需要花费很多精力,尤其是当APC和Memcached之类的加速器正是为此目的而设计时。 (And I wouldn't dismiss this question as such a "strange idea" when some of the largest sites on the internet use Memcached in conjunction with MySQL .) (当互联网上一些最大的站点将Memcached与MySQL结合使用时,我不会把这个问题视为一个“奇怪的想法”)。

That said, zerkms is correct in that you have to fetch the results first, then you can cache the data using APC or Memcached. 也就是说,zerkms是正确的,因为您必须先获取结果,然后才能使用APC或Memcached缓存数据。 There is however another option to caching query results manually, which is to use the Mysqlnd query result cache plugin . 但是,还有另一种手动缓存查询结果的选项,即使用Mysqlnd查询结果缓存插件 This is a client-side cache of MySQL query results. 这是MySQL查询结果的客户端缓存。

The Mysqlnd query result cache plugin lets you transparently cache your queries using APC, Memcached, sqlite, or a user-specified data source. Mysqlnd查询结果缓存插件使您可以使用APC,Memcached,sqlite或用户指定的数据源透明地缓存查询。 However, this plugin currently shares the same limitation as MQC in that prepared statements can't be cached. 但是,此插件当前与MQC有相同的限制,因为无法缓存已准备好的语句。

Why do you need so? 你为什么需要这样? Mysql has its own performant query cache Mysql有自己的性能 查询缓存

but if you still want to follow your strange idea - you need to fetch all the data into array (with mysql_fetch_assoc or whatever) and after that store that array into the memcached. 但是,如果您仍然想遵循自己的奇怪主意-您需要将所有数据提取到数组中(使用mysql_fetch_assoc或其他方法),然后将该数组存储到memcached中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM