Twig: Missing clear() method on the new Twig_CacheInterface?

Created on 14 Oct 2015  路  13Comments  路  Source: twigphp/Twig

From this stackoverflow question:

Since 1.22.0 version of Twig, method Twig_Environment::clearCacheFiles() is deprecated. However, I didn't found anywhere (nor in documentation, nor in repo tickets, nor here on SO) something which can replace this deprecated method.

So what is the idiomatic (and not deprecated) way to clear all cache files now?

Clearing those files via implementing own function seems to be pretty weird.

It really looks like the new interface doesn't contain the clear() method anymore.

Is this intentional?

Most helpful comment

https://github.com/twigphp/Twig/issues/1879#issuecomment-163570408

I would totally agree with that statement under the assumption that you are in a code only project, with certain limitations. Twig itself though seemed to be a library for me.

We in Drupal ran into a complex issue where we believe having such a method would be profitable.

Let's assume you have a multi-webhead setup. For performance reasons you don't want to put the twig template files on a shared filesystem, so each webhead has its own version of the cached files.

We in Drupal have features which make it impossible to generate the cache in a build step because, for example, users can deploy additional bundles via configuration, which can trigger new twig extensions etc. The bundle enabling happens on one of the webheads, so we needed a way to propagate this information to the others.

Long story short: We invalidate the twig cache now based upon a shared prefix for those cache files in a relyable central store, aka. the database.

It would be nice if twig could have a invalidate cache method, which we need, and implement it different then removing the files directly, also given it might be slow.

All 13 comments

Any news? Nobody cares about cache cleaning? =)

The pragmatic solution would be rm -Rf /path/to/twig/cache.

Hard to understand why this has been made: https://github.com/twigphp/Twig/commit/cacfb069b2e65d5487489677238142b464135b40

Let's deprecate method and give nothing back. The Interface even did have clear and it was implemented, but no... Not anymore. @fabpot care to explain?

The argument was that Twig itself will only support filesystem cache (due to opcache). Thus clear() would be the same as removing the cache folder manually. Thus it doesn't need to be in the interface. So you have to clear your cache either manually or write the method yourself.

Ok, then where to get the cache directory properly? In Twig_Cache_Filesystem it's private $directory; as I don't know how long one could trust getCache(true) to return the cache dir? Fearing that at some point it would just return the Twig_Cache_Filesystem.

You are configuring the cache yourself, so you know the cache directory.

$twig->getCache(); will return the cache directory or false.

Closing this issue as @Tobion gave the right answer here.

You are configuring the cache yourself, so you know the cache directory.

3rd parties providers don't configure the loader, so they don't know the loader and the config.
Sad that one use case caused such a BC for such minor benefit.

(and causes everyone else to copy-paste there 'clear cache' logic from project to project)

https://github.com/twigphp/Twig/issues/1879#issuecomment-163570408

I would totally agree with that statement under the assumption that you are in a code only project, with certain limitations. Twig itself though seemed to be a library for me.

We in Drupal ran into a complex issue where we believe having such a method would be profitable.

Let's assume you have a multi-webhead setup. For performance reasons you don't want to put the twig template files on a shared filesystem, so each webhead has its own version of the cached files.

We in Drupal have features which make it impossible to generate the cache in a build step because, for example, users can deploy additional bundles via configuration, which can trigger new twig extensions etc. The bundle enabling happens on one of the webheads, so we needed a way to propagate this information to the others.

Long story short: We invalidate the twig cache now based upon a shared prefix for those cache files in a relyable central store, aka. the database.

It would be nice if twig could have a invalidate cache method, which we need, and implement it different then removing the files directly, also given it might be slow.

It'd be nice if this issue were reopened, because valid concerns have been raised. The assumptions in https://github.com/twigphp/Twig/issues/1879#issuecomment-163570408 only hold for single-web head environments.

Not directly related to this, but: https://wiki.php.net/rfc/url-opcode-cache

One could hopefully soon be able to make other Twig_Cache implementations than Filesystem and get opcache.

Closing again as I don't see what I can do on my side. If anyone has an idea, please feel free to open a new issue to discuss it.

Was this page helpful?
0 / 5 - 0 ratings