tags:

views:

675

answers:

5

Is there a simple way to cache MySQL queries in PHP or failing that, is there a small class set that someone has written and made available that will do it? I can cache a whole page but that won't work as some data changes but some does not, I want to cache the part that does not.

+3  A: 

This is a great overview of how to cache queries in MySQL:

GateKiller
+2  A: 

I think the query cache size is 0 by default, which is off. Edit your my.cnf file to give it at least a few megabytes. No PHP changes necessary :)

lo_fye
+1  A: 

It may be complete overkill for what you're attempting, but have a look at eAccelerator or memcache. If you have queries that will change regularly and queries that won't, you may not want all of your db queries cached for the same length of time by mysql.

Caching engines like the above allow you to decide, on a query-by-query basis, how long the data should be cached for. So say you've data in your header that will change infrequently, you can check if it's currently in the cache - if so, return it, otherwise do the query, and put it into cache with a lifetime of N, so for the next N seconds every page load will pull the data from cache without going near MySQL. You're then free to pull your other data "live" from the db as and when required, by-passing the cache.

ConroyP
+1  A: 

You can use Zend Cache to cache results of your queries among other things.

Željko Živković
A: 

I would recommend the whole page caching route. If some of the data changes, simply place tokens/placeholders in place of the dynamic data. Cache the entire page with those tokens in place, then post process the tokens for the cached data for the tokens. Thus you now have a cached page that contains dynamic content.

buggedcom