Comments (7)
[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.
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.
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.
were you able to implement this group cache?
from phpfastcache.
@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.
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.
See code here: https://github.com/khoaofgod/phpfastcache/issues/59#issuecomment-58953860
from phpfastcache.
Related Issues (20)
- PDO Cache is case insensitive? HOT 4
- Proper way to preload cache items with crontab HOT 14
- Memcached driver compress_data cache miss HOT 4
- Config('path') - Can't declare path from the parent directory of index file directory. HOT 6
- Installation fails: 'Fatal error: Uncaught Error: Class "CacheManager" not found ...' HOT 3
- getItemsByTag() - empty after one item has expired HOT 10
- Can´t retrieve phpfastcache 9.1.3 thru composer HOT 7
- Saving an item that is already in the pool after its expiration time fails HOT 13
- 2 million data cache? HOT 5
- Storing and retrieving mysql result arrays using Psr16Adapter HOT 4
- Deleting cache by tag without loading all its cache HOT 3
- cache not clearing manually in html HOT 2
- Disable 'securityKey' and adapter directory. HOT 9
- Cache clear() not working with Redis, getItem() still returns data HOT 2
- How to use memcache on v8 ? HOT 3
- Support custom native RedisCluster client HOT 9
- TypeError Phpfastcache\Drivers\Files\Driver::driverRead(): Return value must be of type ?array, bool returned HOT 13
- Memory size exhausted by deleting by Tags HOT 10
- Save - Inside workerman event threads Save() throws fopen() error HOT 4
- Redis will only scan 9999 keys in getAllItems HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpfastcache.