I've been expirimenting with the same idea to reuse database-generated pages, etc. Here's how it started:
- I have a "segments" directory with php files. These files output various dynamic segments.
- I have a get_segment(...) function which looks for a useable cached version of a given segment and uses it if it finds one. Otherwise it runs the php file and caches the output. I can specify a maximum cache lifetime, so an older cached version wouldn't be used. Also, get_segment(...) tests to see if the segment's php file has changed since the last cached version was saved.
- If a script changes something that would show up in a segment (i.e. adds a row to a database), it calls an uncache_segment(...) function which basically just deletes the cached version of the segment so that the next time the segment is displayed it must be recreated.
That helped me out in a few situations, but it wasn't enough. One useful addition is that the get_segment(...) function can now take an array of parameters, which can be accessed by the segment's code as array $segment_vars. Different caches are kept for different parameter arrays. When the segment needs to be "uncached" it can either uncache a specific version of the segment (with specific parameters) or uncache all cached versions of the segment.
Then it started to get maybe a little too complicated, because I wanted to be able to save php code in the cached versions so that certain variables could be recreated and used outside of the segment. So in a segment, when it needs to save a variable (for example $num_pages=10), it adds the variable name to an array, $export_vars, like this:
<?
$export_vars[] = 'num_pages';
?>
Later, back in function get_segment(...), it checks the array $export_vars and generates the php code necessary to recreate the variables, in this case:
<?
$GLOBALS['num_pages'] = 10;
?>
It writes this code at the end of the cache file. Then when this file is included, the variables will be recreated. Scalars and arrays can be recreated like this, but of course objects cannot.
The content of the cached file can also be printed with an eval() statement. This is more convenient sometimes. If you ad '?>' to the beginning of the string and '<?' to the end, you can include html text in eval()ed code, and it will be printed regularly.
My last (or most recent anyway) addition to these functions is the ability to pass function get_segment(...) an additional array of "uncached" variables. These don't affect how the segment runs or is cached, and they don't affect which cached version can be used. They are parsed into the segment's outputted html with a regular expression. For example, if a segment includes this html code:
<uv>user_name</uv>
before the code is returned from get_segment(...) it is replaced by the value of $uncached_vars['user_name']. This has been useful for things like that, when I want to include a user-specific variable or something like that, which shouldn't be cached.
I hope I answered your question in there somewhere... I'd like to get some feedback about this system from everyone. I could post the code if you want, but it's not very well commented yet.
-Keegan