Update Version 1.1 is here
For my project I require a caching system, however caching in CI is a all or nothing affair, i.e. I can either cache the entire page or nothing at all.
In real life, this is not sutible. For my site (and I should imagine most sites that are not entirely static) elements on the pages an be divided into those that are cacheable and those that are not (must be generated for each page view).
For example: On all my pages, the message “You are logged in as XXX” is displayed in the top corner of the screen. Obviously, this must be generated for each page view. The rest of the page is (often) cacheable.
Out of the box, CI would not be able to help as it can only cache the entire page or nothing at all.
In order to solve this, I have developed a caching template system that allows you to divide the php blocks into those that can be cached and those that cannot. I have called it ‘SPARKS’ and this is a request for comments. If people like the idea, I will post the code in the wiki.
Basically, you create view files with the following syntax:
<p><?php echo("This block is cacheable"); ?></p>
<p><$?php echo("This block is NOT cacheable"); ?$></p>
Thus, we simply use <$?php ?$> tags for non-cacheable php blocks.
On the first view, the above template gets eval’ed and stored in a cache file. The <$php $> blocks get re-written as <?php ?> blocks. I.e. it becomes:
<p>This block is cacheable</p>
<p><?php echo("This block is NOT cacheable"); ?></p>
So that when this cache file is included, it will execute the non-cacheable php blocks.
Roughly, the pseudo-code for the controller would look like:
1) Load libraries and helpers for non-cacheable content
2) Calculate non-cacheable content
3) Check for a current cached version of the template. If it exists, display it and return.
4) Load libraries and helpers for cacheable content.
5) Calculate cacheable content.
6) Render page to cache.
7) Display page.
Thus is a cached version of the page exists, the controller function returns at step 3 and does not complete steps 4-7.
What do people think of this approach? Its got me out of a sticky situation.
If people think the concept is a good one, I will post the code!