• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sparks : Caching Template system

#1
[eluser]Al James[/eluser]
Update Version 1.1 is here

Hi there,

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:

Code:
<p>&lt;?php echo("This block is cacheable"); ?&gt;</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 &lt;?php ?&gt; blocks. I.e. it becomes:

Code:
<p>This block is cacheable</p>
<p>&lt;?php echo("This block is NOT cacheable"); ?&gt;</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!


Messages In This Thread
Sparks : Caching Template system - by El Forum - 07-14-2007, 02:52 PM
Sparks : Caching Template system - by El Forum - 07-16-2007, 04:41 AM
Sparks : Caching Template system - by El Forum - 07-16-2007, 06:28 AM
Sparks : Caching Template system - by El Forum - 07-23-2007, 04:55 AM
Sparks : Caching Template system - by El Forum - 07-25-2007, 09:28 AM
Sparks : Caching Template system - by El Forum - 07-25-2007, 10:04 AM
Sparks : Caching Template system - by El Forum - 07-25-2007, 10:12 AM
Sparks : Caching Template system - by El Forum - 07-25-2007, 10:17 AM
Sparks : Caching Template system - by El Forum - 07-26-2007, 06:45 AM
Sparks : Caching Template system - by El Forum - 07-26-2007, 08:02 AM
Sparks : Caching Template system - by El Forum - 07-26-2007, 08:07 AM
Sparks : Caching Template system - by El Forum - 07-26-2007, 08:39 AM
Sparks : Caching Template system - by El Forum - 07-26-2007, 09:17 AM
Sparks : Caching Template system - by El Forum - 07-26-2007, 09:21 AM
Sparks : Caching Template system - by El Forum - 07-26-2007, 09:35 AM
Sparks : Caching Template system - by El Forum - 07-26-2007, 03:04 PM
Sparks : Caching Template system - by El Forum - 07-26-2007, 07:39 PM
Sparks : Caching Template system - by El Forum - 07-27-2007, 01:19 AM
Sparks : Caching Template system - by El Forum - 07-27-2007, 05:26 AM
Sparks : Caching Template system - by El Forum - 07-27-2007, 07:10 AM
Sparks : Caching Template system - by El Forum - 08-02-2007, 07:15 AM
Sparks : Caching Template system - by El Forum - 08-13-2007, 12:53 PM
Sparks : Caching Template system - by El Forum - 09-24-2007, 05:32 AM
Sparks : Caching Template system - by El Forum - 09-24-2007, 01:35 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.