Welcome Guest, Not a member yet? Register   Sign In
Partial Cache Library
#1

[eluser]Daeli[/eluser]
Hello everyone,

im happy to present you my work on a partial cache library. It is very basic and could need some polishing. Still i think it is quite finished yet.

To cache a part of a view use the following code:

Code:
Dynamic: &lt;?php echo date('h:i:s')?&gt;<br />

&lt;?php if($this->cache_start('test_case')):?&gt;
Static: &lt;?php echo date('h:i:s')?&gt;
&lt;?php endif; $this->cache_end();?&gt;

Two things are important here. The $this->cache_start() and the $this->cache_end() method.
To tell PHP where to begin the cache you have to call cache_start() with an ID string that identifies the partial cache. E.g. a page number if you use pagination on that partial cache content.
The cache_end() method simply tells PHP where to stop reading the content for the partial cache.
cache_start() also has a a 2nd argument that is the lifetime of the cache in seconds. Default is 60s.

Installation:
Simply put both MY_Output.php and MY_Loader.php in your core folder and you are ready to go.


So, have fun with it!
Here are the files:

Partial_Cache.zip

I don't give a crap about the license. Do whatever you want with it Smile
#2

[eluser]eoinmcg[/eluser]
thanks for sharing.

do you know that there is built in caching since CI2?
http://ellislab.com/codeigniter/user-gui...ching.html
#3

[eluser]Daeli[/eluser]
Yes, but it is not partial caching, right?
#4

[eluser]eoinmcg[/eluser]
yes, it is. before CI2 we just had bare bones file based caching that would cache all output rendered by the controller's method.

now we can save and retrieve items from the cache store. have a look at the manual page, it's very useful particularly as it can use apc, memcached or plain old file system
#5

[eluser]Daeli[/eluser]
I'm not quite sure if you understand what my library does Smile You can just place markers inside a view to "mark" a part of the site to be cached. No need to load views and store them in strings and put them into a cache file.

With this library you are able to cache different content on a site. Say, you want to cache the main content maybe, but not the navigation, as the navigation is personalized. That is when you want to use a partial cache.
#6

[eluser]eoinmcg[/eluser]
i think i understand it but it does lack some advantages of the native drivers. namely;
- the ability to use apc or memcached instead of the file system = much faster
- the ability to set ttl for each item cached
- no need to extend Output or Loader classes

just say i want to display a list of this month's most popular products on a page. i'd first grab the results from the model in my controller then load this data into a view, returning it as a string. if i've already cached it and the cache has not expired i can skip the resource heavy retrieval
Code:
if ( ! $popular_products = $this->cache->get('popular_products'))
{
     $popular_products = $this->products_model->popular_products();

     // Save into the cache for 1hr
     $this->cache->save('popular_products', $popular_products, 3600);
}

$this->data['popular_products'] = $this->load->view('products/popular', array('products' => $popular_products), TRUE);

$this->load->view('main', $this->data);

with your library i'm not too sure how i could handle this elegantly, i.e. moving the query to the view.
#7

[eluser]Daeli[/eluser]
Well, you can output your list, place a marker before and after it and you're done. You can use APC/Memcache with my library - its just not implemented yet - but it should be absolutely easy to do so.

Also you can set the TTL as the 2nd optional parameter of the cache_start function.

If you have some specific paramaters to your list. E.g. the language or something like that, you would just create a unique ID for that partial cache. For example for a gallery on the page 5 with german as the set language you would use $this->cache_start("gallery/$page/$language") wich is enough to identify the cached file.
#8

[eluser]eoinmcg[/eluser]
[quote author="Daeli" date="1305570085"]Well, you can output your list, place a marker before and after it and you're done. [/quote]

in a view, right? my point is that you'll still be retrieving the the list from the db in your controller via a model. this means your still have the overhead of running the query on every request.
#9

[eluser]Daeli[/eluser]
Yes, there is a function cache_exists($id) wich you can call in your controller with the id wich returns TRUE if the part is cached. So you can avoid the database calls.
#10

[eluser]eoinmcg[/eluser]
Quote:Yes, there is a function cache_exists($id) wich you can call in your controller with the id wich returns TRUE if the part is cached. So you can avoid the database calls.

my bad, didn't see that.




Theme © iAndrew 2016 - Forum software by © MyBB