Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter caching library (with tagging!)
#1

[eluser]alexbilbie[/eluser]
Hi everyone,

I've written a caching library for CodeIgniter and I'm hoping some people could give it a spin for me to see if they can find any bugs I may have missed. The idea is that you can cache anything and additionally tag it so deleting a number of similar caches is dead easy.

The code is here http://alexbilbie.com/code/Cache.phps (remember to rename the extension to .php)

Usage:

Add something to the cache (returns TRUE if successful)

Code:
$this->cache->set( (string)$id, (mixed)$data, (array)$tags, (int)$lifetime );

Get something from the cache (returns FALSE if fail)

Code:
$this->cache->get( (string)$id );

Delete an item

Code:
$this->cache->delete( (string)$id );

Delete an item by tag (accepts either one tag or an array of tags)

Code:
$this->cache->delete_by_tag( (string|array)$tags );

Delete all

Code:
$this->cache->delete_all();

I'll update the file as people find bugs.

I hope people find it useful! :-)

Alex
#2

[eluser]alexbilbie[/eluser]
A few things I forgot to mention:

Firstly this library sits alongside CodeIgniter's own output and database caching. The delete_all() function in this library will only delete it's own cache files, it won't delete caches created by the Output or Database class.

Secondly the only the thing I've not finished is some internal self cleaning. When you delete an individual cache file or the library discovers that something you've requested is out of date it deletes the file and removes it's pointer in the lookup file but it doesn't remove itself from the tag lookup array. However deleting by tag or deleting everything does clean the tag lookup array. It's not a major bug by any means, and I will add it in at some point.
#3

[eluser]Seppo[/eluser]
Hi,
just reading the code, I didn't teasted, but a few notices.

1) You are forcing a cache-file method... May be you can define the methods and use heritage to define different methods (files, memcache, database...) something like CI database driver orientation.
2) the unset($CI) line really scares me... you are actually not wasting memory, since the $CI object is the same controller everywhere, you only have one reference there
3) Serializing resources does nothing usefull (according to PHP.net: "The value to be serialized. serialize() handles all types, except the resource-type."). Probably issue a log_message("error", ...) there would be better
4) I'm not quite sure why serializing only object and arrays and storing a flag, when you can actually serialize all values.
5) Is necesary to use the time for the key? Why not just the id? that way you will be handling the overwrite easier
6) If the cache key does not exists, probably returning NULL will be more appropiate than FALSE... NULL is usually used for undefined vars.
7) delete/delete_all return TRUE if it has data and NULL otherwise... unusual logic.
8) every method you have calls read_file($this->cache_handler_filepath), which seems repetitive and not-optimal... maybe a read_data method would open the file, read it, and keep in memory so the second call wouldn't?
9) same principle applies when writing the handler, you have a lot of duplicate code there (and sometimes you log an errors, and sometimes you don't), probably extracting a method to store the handler will be useful
#4

[eluser]alexbilbie[/eluser]
@seppo many thanks for your reply.

1) You're right I could abstract this out a bit more, however in my situation I'm without access to other methods such as memcache (which if I understand correctly wouldn't implement the tagging feature) and I wanted to purposely not cache in a database due to my current situation of having limited server resources.

2) Fair point

3+4) Perhaps I misunderstood the intended use of serialize but I was thinking that if you're just caching a name or some html for example it would be less intensive to literally store it as that as opposed to serializing to unserialize it. Therefore I only serialized objects and arrays.

5) I don't understand what you mean here, the ID is the key?

6) I'll make this change

7) My thinking was that if you ask to something to be deleted you're just going to presume it's deleted as it's unlikely you will call it again (because you're under the impression it's been deleted).

8+9) Yes I agree, again this is something I intend to change.
#5

[eluser]Seppo[/eluser]
[quote author="alexbilbie" date="1258344546"]1) You're right I could abstract this out a bit more, however in my situation I'm without access to other methods such as memcache (which if I understand correctly wouldn't implement the tagging feature) and I wanted to purposely not cache in a database due to my current situation of having limited server resources.[/quote]
Well... if you use memcache you can have tagging feature as u have now, making you own indexing/lookup system.

[quote author="alexbilbie" date="1258344546"]3+4) Perhaps I misunderstood the intended use of serialize but I was thinking that if you're just caching a name or some html for example it would be less intensive to literally store it as that as opposed to serializing to unserialize it. Therefore I only serialized objects and arrays.[/quote]
3) Again, serializing a resource is an error, and completly wrong and useless.
4) You might add some small overhead, but if you store a boolean or an integer you will be keeping the type which is useful.

[quote author="alexbilbie" date="1258344546"]5) I don't understand what you mean here, the ID is the key?[/quote]My bad here, I meant the file name, not the key

[quote author="alexbilbie" date="1258344546"]7) My thinking was that if you ask to something to be deleted you're just going to presume it's deleted as it's unlikely you will call it again (because you're under the impression it's been deleted).[/quote]well, usually makes sense that if you return a true you would return false and not null..
#6

[eluser]alexbilbie[/eluser]
Okay I've updated the library following suggestions from @seppo.

I've modulised the code a bit to reduce code repetition, added some self cleaning functions, fixed a bug or two I'd originally missed and I've added a new function:

Code:
$this->cache->cleanup();

...which can be run on a cron job which will remove cache files which are no longer valid.




Theme © iAndrew 2016 - Forum software by © MyBB