asm89 / twig-cache-extension

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

[WIP] Cache Digests #23

Closed CHH closed 8 years ago

CHH commented 9 years ago

When working with designers it would be convenient if you could omit the versioning portion of the cache key, so you can use {% cache 'user' %} instead of {% cache 'user/v5' %}.

The problem is, that if you have a complex structure of templates, where a tree of other templates is included inside the cache tag, then you have to version the key every time you change one of the included templates. Sometimes someone forgets to do this and this leads to unexpected results.

Rails handles this by automatically including the hash of the contents and by all templates which are included as partials inside the cache tag. That's called "cache digests" and I found a post which also describes this problem: http://blog.remarkablelabs.com/2012/12/russian-doll-caching-cache-digests-rails-4-countdown-to-2013

What do you think of this @asm89?

asm89 commented 9 years ago

@CHH Looks interesting indeed. Wondering if it's possible to get the data we need from twig in a sensible way.

I'd be happy to have support for this in the extension.

CHH commented 9 years ago

@asm89 I think I've got it that far that I'm getting all the needed info in a sensible way and it's working. It's more of a proof of concept currently, so the code isn't very beautiful, but what do you think?

One major issue is that when changing a template in development which gets included in a cache block, then the digest is not rebuilt. I've no idea how Twig's cache works (especially in dev) so help would be greatly appreciated here.

asm89 commented 9 years ago

@CHH Not sure about the digest not being rebuild. My guess is that twig only recompiles files that are changed. What you're looking for is also getting the templates that depend on it recompiled. I'm guessing that'll need changes to twig core. It might be possible to get support for it in twig 2.0, but I'm not sure if there are legitimate uses apart from this extension (supporting it wouldn't be too hard I think though).

CHH commented 9 years ago

@asm89 I've now changed the digest generation to be at runtime, and use sha1_file on the template source files. This invalidates the cache when an included template changes, and didn't have any meaningful performance impact, because it should hit the FS cache.

CHH commented 9 years ago

@asm89 but I would really like to have a more efficient way to generate the digests of the included templates, maybe something which uses the cached template classes?

asm89 commented 9 years ago

@CHH my point is that recompilation could/should happen in twig core. Digests of included templates at runtime is a no go imo, especially in production mode. Might be a feasible hack to get it working in dev.

CHH commented 9 years ago

@asm89 makes sense to avoid digests at runtime. My point being: After looking through Twig's code, I don't think that core can handle that case. But I'd like to be proven wrong.\

Edit: The least disruptive change to Twig, and of use for this, would be when Twig would expose a digest of the template source in the Template class. Something like $template->getDigest().

asm89 commented 8 years ago

@CHH do you still want to pursue this?

asm89 commented 8 years ago

Closing for now. :)