asm89 / twig-cache-extension

The missing cache extension for Twig. Caching template fragments with Twig.
MIT License
388 stars 27 forks source link

nested generational cache #47

Open patie opened 7 years ago

patie commented 7 years ago

hi, its somehow possible something like this?

{% cache 'product' product %}
    {{ product.id }}
    {% cache 'category' category %}
        {{ category.id }}
    {% endcache %}
{% endcache %}

when i change category (i have own key generator, watching classname+id+updatedat, this is ok), i want change/regenerate only category block inside - but its not working for me, its possible to do this or i must regenerate top parent - product in this case? thanks

rvanlaak commented 7 years ago

Yes nesting is possible, so the problem you face is related to the cache invalidation.

Obviously if you only invalidate the inner cache item, the parent cache item still is there.

So, your category strategy should also invalidate the product strategy.

patie commented 7 years ago

so when i want invalidate inner/nested item i must invalidate all parents? no other way? not very good idea invalidate all products in category, because category url changed :)

rvanlaak commented 7 years ago

That is the concept of caching I guess? Both the parent and child are not aware that they have an interrelated dependency.

One way to solve this, would be to group your cache items by tagging them, and invalidate using tags. Not all cache systems support tags though.

patie commented 7 years ago

@rvanlaak i understand its concept of russian dolls caching, but im searching solution for solve this, tagging will be great, but i think in this library itsnt support for tagging and invalidating by tag, or im wrong?

rvanlaak commented 7 years ago

You can use the PSR-6 adapter provided with this library, and for instance use Redis as that supports tags.

patie commented 7 years ago

can you please show simple example with tagging for this library?

rvanlaak commented 7 years ago

It would be a combination of using the Twig cache PSR-6 adapter and using a cache pool implementation adapter that supports tagging.

Although I did not implement tagging myself yet, it seems like all cache pools nowadays support tagging via the TagSupportWithArray trait.

patie commented 7 years ago

so must be implement, not work out of the box, ok

rvanlaak commented 7 years ago

@patie please remember that this library is about twig caching, not about any business rules around cache invalidation using grouping by tag. As I probably am the only one reading this issue, I also think that posting your question on Stackoverflow will help you better, as it will get more exposure over there. 👍

patie commented 7 years ago

no problem, im only asking, thank you :)