2

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;
Anders
  • 8,307
  • 9
  • 56
  • 88
Roch
  • 21,741
  • 29
  • 77
  • 120

2 Answers2

9

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's Query Cache is great, but it still has limitations:

  • 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

Even with a remote server, Memcached is roughly 23% faster than MQC. And using APC's object cache, you can get up to a 990% improvement over using MQC alone.

So there are plenty of reasons to cache database result sets outside of MySQL's Query Cache. After all, you cache result data locally in a PHP variable when you need to access it multiple times in the same script. 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. (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.)

That said, zerkms is correct in that you have to fetch the results first, then you can cache the data using APC or Memcached. There is however another option to caching query results manually, which is to use the Mysqlnd query result cache plugin. This is a client-side cache of MySQL query results.

The Mysqlnd query result cache plugin lets you transparently cache your queries using APC, Memcached, sqlite, or a user-specified data source. However, this plugin currently shares the same limitation as MQC in that prepared statements can't be cached.

Lèse majesté
  • 7,923
  • 2
  • 33
  • 44
-2

Why do you need so? Mysql has its own performant query cache

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.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • 3
    I don't want my application to query my database for the same thing twice. – Roch Feb 08 '11 at 10:39
  • @mnml: is not a memcached a storage too? What is the difference between querying mysql (and retrieve data from query cache) and retrieving from memcached? ;-) Anyway, you should extract all the data from the resource and cache the result array. – zerkms Feb 08 '11 at 10:41
  • 2
    It makes a difference when you have more than one server: it reduces the network traffic between the webservers and the databases and it's also faster once the query is in cache. – Roch Feb 08 '11 at 10:47
  • @mnml "i don't want" is more like whim rather than sensible reason. do you have one? – Your Common Sense Feb 08 '11 at 10:50
  • @mnml reasons you want scalability? all scalability problems (which are numerous) already solved? 128M session storage from your previous question is for scalability too? – Your Common Sense Feb 08 '11 at 10:57
  • 2
    check this - http://stackoverflow.com/questions/815041/memcached-vs-apc-which-one-should-i-choose – Jon Black Feb 08 '11 at 11:02
  • @mnml: "it reduces the network traffic between the webservers and the databases" --- and increases the traffic amount on the same value between frontend and memcached. "and it's also faster once the query is in cache" --- why do you think so? It is faster, when you're performing slow (not optimized) queries many times. – zerkms Feb 08 '11 at 11:06
  • @mnml: on my small home server (with default config and without tuninag at all) I can easily perform 6000 requests per second. Is not that enough for you? – zerkms Feb 08 '11 at 11:07
  • "Yes, right now it's scalability" --- scalability and performance are different terms and being achieved with **different** techniques. – zerkms Feb 08 '11 at 11:08