Cache dot notation dependency support
Hey everyone
project I'm working on now, requires some dependency while invalidating cache etc. And it became annoying to add those myself everywhere.
since cache already supports dependency invalidation, I have extended cache class so it does support invalidation based on identified key, which understands dots as separation of dependency...
Lets get to the example:
We have cached users
We have cached users.1
We have cached users.1.photos
so if we delete users.1 - users.1.photos won't get invalidated...
same if we delete users cache, nor users.1 neither users.1.photos won't be invalidated, so for now I have extended core class like this:
http://bin.fuelphp.com/snippet/view/ew/valRgIE1vBX975CgMhoS
what it basically does, it searches for dot (.) in the identifier, and builds dependency on it.
if we pass:
users.1.photos, it sets users.1 and users as a $dependency
if we pass:
users.1 it sets users as a $dependency...
As you can see, it goes with levels down:)
and now if we remove users identifier, all keys which depend on it, will be invalidated.
not sure how backwards compatible it is and how project specific it is... but it makes sense, since everywhere in Arr class, we have dot as a separator.
Thanks
+1
Interesting idea @huglester. I didn't even know about that feature in the Cache class.
Would it be good for get() to also have dot notation?
@dmyers ::get() already support that you just can Cache::get('users.1'), but I don't understand what should change in get? or be added?
Thanks
This requires more thought. The idea is great, but I don't like the automatic mechanism.
It would break ALL our applications, which use a cache sub-folder structure to organize the cache, but items in there aren't really dependent on each other.
Not only a delete, but also an update would invalidate the cache dependencies. So in this example an update of 'users' would invalidate the cached photo's, which is unintended behaviour I think...
One option would be to use a config item, but that is a global setting and not (always) desired as well.
Well probably a config option would be best here as you've said.
I personally can't think of better option.
"So in this example an update of 'users' would invalidate the cached photo's, which is unintended behaviour I think..."
Well I think it probably more of personal preference... if I wanted profile photos to be non users.1. dependent.. I'd Just name a cache key like: users_1_photos and that's all
Because I try to build a doted key, if there is a hierarchy, if no hierarchy, then smth like users_1_photos, or just photos.1 etc...
The main reason for using dot-notation (in case of file drivers) is the ability to organize into a tree structure.
If not, all files would be in the same folder, which with some of our applications would mean 100.000+ files in a single directory. Something you would absolutely want to avoid.
So we use a key structure like "module.function.item.optional_subitem" to keep the cache system ok performance wise.
Also.... there's a maximum for number of files in one directory, which is high but reachable. When you take this into account when using cache you're able to prevent this.
Rereading this again:
This functionality is implemented using the delete_all() method, delete_all('users') would achieve what is required here, which is delete the entire 'users' tree from the cache.