CodeIgniter Forums

Full Version: Get / delete all cache starting with prefix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.