-
Notifications
You must be signed in to change notification settings - Fork 27
How to invalidate cache when data change? #50
Description
Hi,
we want to cache templates that use lazily fetched data. Something like this:
class ViewModel
{
public function getItem(): Item
{
$this->repository->findItem();
}
}{% cache 'v1/show_item' viewModel %}
{{ viewModel.item.title }}
{$ endcache %}Of course, most of the time our logic is more complicated than that. There is more template stuff to cache and more data stuff to cache. By using VM inside cached part of template, we have a lazily loaded entity (or entities, or other data), which seems cool enough to use this extension.
The problem is: how do we invalidate the cache when values in Item change?
We could use GenerationalCacheStrategy, but that would mean we cannot have Item (and other stuff) lazily–loaded.
So we want to invalidate cache „manually” (but still in code ;)) when necessary. For example, there is code that changes title in some Item – then we'd like to call something like this: invalidateCache('show_item_'.$item->getId()).
But what if there are more parameters to take care of? Like current user status, etc?
Should we try cache tags? What's the best way to implement them here?
Any other ideas?
As always,
There are only two hard things in Computer Science: cache invalidation and naming things.
-- Phil Karlton