There are only two hard things in Computer Science: cache invalidation and naming things.
-- Phil Karlton
Here is how I handle this problem in most of my projects, not the best, but you can use it as an idea and improve ;)
All my models extend a BaseModel
which has clearCacheFiles
method:
private function clearCacheFiles()
{
// Clear Filesystem cache
$modelNamespaces = explode('\\', get_class($this));
$model = strtolower(end($modelNamespaces));
$path = $this->getDI()->getConfig()->site->path->cache . 'queries/';
\Helpers\Files::deleteFileLike($path . $model .'-');
}
public function afterSave()
{
$this->clearCacheFiles();
}
public function afterDelete()
{
$this->clearCacheFiles();
}
How does it work? Every time a model is saved, deleted and so on the clearCacheFiles
method is called, which deletes the actual cache files.
Whenever I need custom cache handling for a model, i just overwrite the clearCacheFiles
in the desired model.
For example, the cache key for a query to fetch all products would look something like products-all
, the important part in my case is that I use the model name for cache "prefix" to automate deletion.
- Specific product
products-one-43
- Complex search:
products-search-'. md5(serialize($params))
Where $params
can hold any amount of search parameters
- Your example:
[email protected]