Part of the EllisLab Network
   
1 of 2
1
Cache library
Posted: 24 July 2007 02:22 AM   [ Ignore ]  
Grad Student
Rank
Total Posts:  42
Joined  07-14-2007

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

Profile
 
 
Posted: 24 July 2007 08:02 AM   [ Ignore ]   [ # 1 ]  
Summer Student
Total Posts:  24
Joined  06-14-2007

Thanks for this, very useful

Profile
 
 
Posted: 25 July 2007 05:01 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  320
Joined  07-23-2006

This looks like just the thing I need, thanks!

 Signature 

running man webdesign

Profile
 
 
Posted: 25 July 2007 10:30 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Rank
Total Posts:  95
Joined  10-24-2006

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?

Profile
 
 
Posted: 25 July 2007 11:03 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  42
Joined  07-14-2007

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://codeigniter.com/forums/viewthread/56456/)

Profile
 
 
Posted: 26 July 2007 02:57 AM   [ Ignore ]   [ # 5 ]  
Grad Student
Rank
Total Posts:  44
Joined  05-17-2007

good

 Signature 

l[url=http://www.angeltt.com

]MySite study from here with CodeIgniter—for my angel[/url]

Profile
 
 
Posted: 26 July 2007 01:49 PM   [ Ignore ]   [ # 6 ]  
Summer Student
Total Posts:  18
Joined  06-03-2006

Nice Class! wink

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

Profile
 
 
Posted: 26 July 2007 01:55 PM   [ Ignore ]   [ # 7 ]  
Grad Student
Rank
Total Posts:  42
Joined  07-14-2007

@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…

Profile
 
 
Posted: 13 August 2007 02:33 PM   [ Ignore ]   [ # 8 ]  
Grad Student
Avatar
Rank
Total Posts:  74
Joined  12-20-2006

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!

Profile
 
 
Posted: 28 December 2007 05:44 AM   [ Ignore ]   [ # 9 ]  
Grad Student
Rank
Total Posts:  89
Joined  06-05-2006

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

Profile
 
 
Posted: 24 February 2008 01:24 AM   [ Ignore ]   [ # 10 ]  
Grad Student
Rank
Total Posts:  62
Joined  06-11-2007

Nah, it works just great for me.

Profile
 
 
   
1 of 2
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 719, on June 06, 2008 10:16 AM
Total Registered Members: 77565 Total Logged-in Users: 20
Total Topics: 101557 Total Anonymous Users: 1
Total Replies: 544401 Total Guests: 209
Total Posts: 645958    
Members ( View Memberlist )
Newest Members:  semperjrawhallshiusbozzlynobluffkatiejameshsmith101dddougalcampinggreaterphoenix54