Giter Club home page Giter Club logo

Comments (7)

spud avatar spud commented on July 28, 2024

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

from phpfastcache.

tylercollier avatar tylercollier commented on July 28, 2024

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.

from phpfastcache.

tylercollier avatar tylercollier commented on July 28, 2024

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.

from phpfastcache.

rajneeshojha avatar rajneeshojha commented on July 28, 2024

were you able to implement this group cache?

from phpfastcache.

tylercollier avatar tylercollier commented on July 28, 2024

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

from phpfastcache.

rajneeshojha avatar rajneeshojha commented on July 28, 2024

can you please share the code? Regards
Rajneesh Ojha

 On Thursday, July 23, 2015 2:51 AM, Tyler Collier <[email protected]> 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.

from phpfastcache.

tylercollier avatar tylercollier commented on July 28, 2024

See code here: https://github.com/khoaofgod/phpfastcache/issues/59#issuecomment-58953860

from phpfastcache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.