Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear section of cache #59

Closed
tylercollier opened this issue Oct 13, 2014 · 7 comments
Closed

Clear section of cache #59

tylercollier opened this issue Oct 13, 2014 · 7 comments

Comments

@tylercollier
Copy link

I'd like to clear a section of my cache. Say for example that I store data by prefixing it with a namespace. Perhaps I cache some twitter data, facebook data, etc. I might store them as twitter.myvalue1, twitter.myvalue2, and facebook.myvalue1, facebook.myvalue2. How could I delete the cache entries for twitter? And I don't know all the names of the values. It's not necessary myvalue1/2/3/etc, I just know it starts with twitter.

@spud
Copy link

spud commented Oct 13, 2014

[Edit: 22 July 2015 to fix inconsistency in variable names, for clarity]

I was trying to figure out the same problem...

I cache individual pages, for example, but I also cache "chunks" of data based on what module they belong to. And sometimes I'd like to clear the cache of any data that belongs to a certain module.

Some individual caches allow you to "group" cache items like this, but for flexibility with a generic caching system, you have to be a bit more creative.

In my case, I kept a second cache, whose key was a consistent module identifier (e.g. "cache.twitter"), and each value in the cache corresponded to another cache key.

So I would, for example,

// Save a twitter module value:
twitter.myvalue1 = "foo";
// Save the key name in another array that holds all the "twitter" module cache keys
cache.twitter = "twitter.myvalue1";

// ..and repeat, adding the new key to the existing array of "twitter" keys
twitter.myvalue2 = "bar";
cache.twitter = "twitter.myvalue1,twitter.myvalue2"; // this would probably be an array, not a list

// ..and repeat, adding the new key to the existing array of "twitter" keys
twitter.myvalue3 = "baz";
cache.twitter = "twitter.myvalue1,twitter.myvalue2,twitter.myvalue3"

Now, when you want to clear the entire "twitter" cache, you do this (in pseudo-code)

$keys = $Cache->get("cache.twitter");
foreach($keys as $key) {
$Cache->delete($key);
}
// And delete the cache item that held all the keys
$Cache->delete("cache.twitter");

If you write simple functions to encapsulate this functionality, it's pretty flexible and easy to integrate.

spud.

On Oct 13, 2014, at 2:50 PM, tylercollier wrote:

I'd like to clear a section of my cache. Say for example that I store data by prefixing it with a namespace. Perhaps I cache some twitter data, facebook data, etc. I might store them astwitter.myvalue1, twitter.myvalue2, and facebook.myvalue1, facebook.myvalue2. How could I delete the cache entries for twitter? And I don't know all the names of the values. It's not necessary myvalue1/2/3/etc, I just know it starts with twitter.

@tylercollier
Copy link
Author

Great idea. I just implemented your idea and it works great.

I ended up using the term tag instead of module, because like gmail tags, I might want to tag a cache value with multiple tags. If I only ever wanted to assign a value to a particular module, I think that could be accomplished with the 'securityKey' (which only works for files and PDO drivers; I'm using the files driver) concept already present in phpfastcache.

@tylercollier
Copy link
Author

I have not yet tested this in the real world. I'm nervous about how long $cache->delete() calls will take in a loop with many keys. I'll report back after I get some real world testing.

@rajneeshojha
Copy link

were you able to implement this group cache?

@tylercollier
Copy link
Author

@rajneeshojha: yes. I did it just like spud suggested. It was very simple. It's in my own code, not part of the phpfastcache library, so I did not do a pull request or anything.

@rajneeshojha
Copy link

can you please share the code? Regards
Rajneesh Ojha

 On Thursday, July 23, 2015 2:51 AM, Tyler Collier <notifications@github.com> wrote:

@rajneeshojha: yes. I did it just like spud suggested. It was very simple. It's in my own code, not part of the phpfastcache library, so I did not do a pull request or anything.—
Reply to this email directly or view it on GitHub.

@tylercollier
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants