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

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


Messages In This Thread
Cache library - by El Forum - 07-24-2007, 01:22 AM
Cache library - by El Forum - 07-24-2007, 07:02 AM
Cache library - by El Forum - 07-25-2007, 04:01 AM
Cache library - by El Forum - 07-25-2007, 09:30 AM
Cache library - by El Forum - 07-25-2007, 10:03 AM
Cache library - by El Forum - 07-26-2007, 01:57 AM
Cache library - by El Forum - 07-26-2007, 12:49 PM
Cache library - by El Forum - 07-26-2007, 12:55 PM
Cache library - by El Forum - 08-13-2007, 01:33 PM
Cache library - by El Forum - 12-28-2007, 04:44 AM
Cache library - by El Forum - 02-24-2008, 12:24 AM
Cache library - by El Forum - 07-18-2008, 03:13 AM
Cache library - by El Forum - 12-17-2008, 04:38 PM
Cache library - by El Forum - 12-17-2008, 05:19 PM
Cache library - by El Forum - 06-11-2009, 12:09 AM
Cache library - by El Forum - 09-18-2009, 02:02 PM
Cache library - by El Forum - 11-04-2009, 03:12 AM
Cache library - by El Forum - 04-12-2011, 02:40 AM



Theme © iAndrew 2016 - Forum software by © MyBB