Welcome Guest, Not a member yet? Register   Sign In
Cache library

[eluser]Al James[/eluser]
Hi there...

I have just written a data cache library for my own work. I thought I would share it here.

Briefly it supports storing and loading arbitrary PHP objects to a file based cache. Objects are identified by a cache id and (optionally) a cache group. Cache groups allow you to delete all cache items with a particular group.

Eventually I want to set up some kind of variable backend solution where higher performance backends can be chosen (APC shared mem or memcached) without modifying the interface.

Here is how to use it (in Controller):

if (!$data = $this->cache->get('my-cache-id'))
echo("Cache miss!");
//This would be a really 'heavy' object (slow to generate)
$data = array('my' => 'object');
$this->cache->save('my-cache-id', $data);


Cache grouping is powerful. I.e. in my site, I could have cache groups corresponding to the main sections i.e. 'blogs', 'forums' etc... And then for these sections, for example 'forums', I could put the contents of the forum index pages and store them with cache ids 'page1', 'page2' etc... Then, when I want to update all cache files corresponding to forum indexes, I simply delete the cache group 'forums'.

E.g. in thread index controller:

$page = 1; //Get from url
$cache_id = 'page'.$page;

if (!$data = $this->cache->get($cache_id, 'forums'))
$threads = $this->Threads->get_threads($page);
$this->cache->save($cache_id, $data, 'forums', 600); //Live for 10 mins


$data['threads'] = $threads;

//Do view

To clear the forum caches:

//Clear all forums cache files. Will force regeneration next view.

Anyway, let me know what you think...

UPDATE: Forgot to post link to code: http://codeigniter.com/wiki/File:Cache.0.9.zip/

[eluser]James Spibey[/eluser]
Thanks for this, very useful

This looks like just the thing I need, thanks!

oh wow.. i saw you're the same person who wrote the sparks library i just found on the wiki Smile is this integrated with sparks, or are they two completely different libraries?

[eluser]Al James[/eluser]
Hi there...

At the moment they are separate, but I will release a new version of the sparks library very soon that will use the cache library...

Any suggestion (best to put them in the sparks thread on the forum: http://ellislab.com/forums/viewthread/56456/)


Nice Class! ;-)

Cache_Lite is a similar data cache system with excellent anti-corruption tests.

[eluser]Al James[/eluser]

Yes I have used Cache_Lite myself, but wanted a solution that does not require PEAR (yuk!)... You right about the anti-corruption tests. Currently mine used file locking, but cache lite also (optionally) allows file hashing... I might consider adding this if there is demand...

Thanks very much for this library... Sparks was a little much for me but I found this and wow it works perfect for what I am trying to accomplish!

Does anyone else have trouble implementing this?
I just keep getting an all blank page...

Theme © iAndrew 2016 - Forum software by © MyBB