Welcome Guest, Not a member yet? Register   Sign In
Cache wildcards - Find / delete all cache starting with prefix
#1

(This post was last modified: 04-05-2021, 02:00 AM by yassine.)

Hey,

I'm working on a project that relies heavily on cache for performance gains. All the cache created is intentionally set to expire in a DECADE and deleted upon user/system action. When there are a few cache keys to handle with a few dependencies between the cached objects, there is no problem... Although, the more cache keys and dependencies there are to handle, the harder it gets to manage it and at some point, it creates inevitable bugs.

CodeIgniter offers some basic ways to get and delete single cache objects when the key is known (https://codeigniter4.github.io/userguide...ching.html).
--> Regarding the project I'm working on, the cache I create is named manually with some identifiers from the database to ensure unique cache keys. So, the hassle comes when wanting to delete it as I need to get the identifier back from the database.

A solution to this would be to add a method to find or delete all cache of which keys are starting with a given prefix.

PHP Code:
// find all cache starting with `prefix_`
cache()->findAll("prefix_*");
// would return an array: ["prefix_...", "prefix..."];

// delete all cache starting with `prefix_`
cache()->deleteAll("prefix_*"); 

I've seen that there is a `$prefix`attribute that can be set to prefix any created cache key, it could be merged somehow with the `clean()` method that removes the entire cache.

PHP Code:
// This would remove all the cache starting with "prefix"
cache()->prefix("prefix")->clean(); 

This would help greatly remove the load on the database (in my case) as well as maintaining cache more easily.

That's it, hope to have been clear!

PS: I've been using CodeIgniter4 for a few months now and I'm quite fond of it! Thanks for all your work.
Reply
#2

I'm a cache noob so will rely on others' expertise, but these methods seem appealing assuming they can be handled in all drivers and done efficiently.
Reply
#3

(04-05-2021, 04:51 AM)MGatner Wrote: I'm a cache noob so will rely on others' expertise, but these methods seem appealing assuming they can be handled in all drivers and done efficiently.

Thanks for the reply! I think this might interest some people. Also, I did some research and found that it could be done easily in the majority of handlers. Memcached will be the painful one apparently! I'll work on a PR in the coming days.
Reply
#4

Hello, that great idea. You can use as example my code for CI3 https://github.com/WitER/codeigniter-wildcard-cache
Reply
#5

(04-09-2021, 12:23 PM)WitER Wrote: Hello, that great idea. You can use as example my code for CI3 https://github.com/WitER/codeigniter-wildcard-cache

Just saw your message! Thanks for the example.

I've created the PR: https://github.com/codeigniter4/CodeIgniter4/pull/4567
Reply




Theme © iAndrew 2016 - Forum software by © MyBB