CodeIgniter Forums

Full Version: Cache library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[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):

Code:
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:

Code:
$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:

Code:
//Clear all forums cache files. Will force regeneration next view.
$this->cache->remove_group('forums');

Anyway, let me know what you think...

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

El Forum

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

El Forum

[eluser]frenzal[/eluser]
This looks like just the thing I need, thanks!

El Forum

[eluser]jbowman[/eluser]
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?

El Forum

[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/)

El Forum

[eluser]whidbey[/eluser]
good

El Forum

[eluser]Unknown[/eluser]
Nice Class! ;-)

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

El Forum

[eluser]Al James[/eluser]
@Sen

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...

El Forum

[eluser]eedfwChris[/eluser]
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!

El Forum

[eluser]Sarre[/eluser]
Does anyone else have trouble implementing this?
I just keep getting an all blank page...
Pages: 1 2