I am currently building a PHP framework (original, i know) and im working on some optimisation features for it. One dilema I have come accross is what is the best way to ca
Memcached.
I always try to create my own solution at least once, to grasp better what's going on under the hood in most situations. When I created my own caching solution, I essentially did what you're talking about.
// serialize an array of all results
$serialzedData = serialize($resultData);
// set TTL (60 seconds) and create cache filename with timestamp
$ttl = 60;
$cacheFilename = $ttl . '_' . time() . '_' . md5($sqlQuery)
// dump
file_put_contents($cacheFilename, $serializedData);
Prior to a query firing, it would search the cache
directory for files with a matching query hash. If it does, it tests timestamp + ttl <= current_time
, and if true, returns the unserialized file contents. Otherwise, overwrite it.
Mysql caches result of query, may be you should increase mysql query cache size? Or cache result of big query in standalone table?
If this is a straight array, then you could use var_export() rather than serialize (wrapping it with the appropriate "" and write it to a .php file; then include() that in your script. Best done if you can write it outside the htdocs tree, and only really appropriate for large volumes of data that memory caches would consider excessive.
Google for Memcache, that should put you in the right direction.