From 96a54b3f40a2cc0eda54b5e117ea73e6a995760f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 13 Sep 2015 12:11:42 +0200 Subject: [PATCH] added an option to force PHP bytecode invalidation when writing a compiled template into the cache --- CHANGELOG | 1 + doc/recipes.rst | 22 ++-------------------- lib/Twig/Cache/Filesystem.php | 16 +++++++++++++++- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 0b070a4b3..b808f17a1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.22.0 (2015-XX-XX) + * added an option to force PHP bytecode invalidation when writing a compiled template into the cache * fixed the profiler duration for the root node * changed template cache names to take into account enabled extensions * deprecated Twig_Environment::clearCacheFiles(), Twig_Environment::getCacheFilename(), diff --git a/doc/recipes.rst b/doc/recipes.rst index 1473b201c..9461cae72 100644 --- a/doc/recipes.rst +++ b/doc/recipes.rst @@ -339,28 +339,10 @@ When using OPcache with ``opcache.validate_timestamps`` set to ``0`` or APC with ``apc.stat`` set to ``0`` and Twig cache enabled, clearing the template cache won't update the cache. -To get around this, create a custom ``Twig_Cache_Interface`` implementation and -force the update of the cache when Twig rewrites the cache:: - - class OpCacheAwareCacheFilesystem extends Twig_Cache_Filesystem - { - public function write($key, $content) - { - parent::write($key, $content); - - // Compile cached file into bytecode cache - if (function_exists('opcache_invalidate') && ini_get('opcache.enable')) { - opcache_invalidate($key); - } elseif (function_exists('apc_compile_file') && ini_get('apc.enabled')) { - apc_compile_file($key); - } - } - } - -Then, use that new class as the template cache:: +To get around this, force Twig to invalidate the bytecode cache:: $twig = new Twig_Environment($loader, array( - 'cache' => new OpCacheAwareCacheFilesystem('/some/cache/path'), + 'cache' => new Twig_Cache_Filesystem('/some/cache/path', Twig_Cache_Filesystem::FORCE_BYTECODE_INVALIDATION), // ... )); diff --git a/lib/Twig/Cache/Filesystem.php b/lib/Twig/Cache/Filesystem.php index f3946016a..8675e0750 100644 --- a/lib/Twig/Cache/Filesystem.php +++ b/lib/Twig/Cache/Filesystem.php @@ -16,14 +16,19 @@ */ class Twig_Cache_Filesystem implements Twig_CacheInterface { + const FORCE_BYTECODE_INVALIDATION = 1; + private $directory; + private $invalidateBytecode; /** * @param $directory string The root cache directory + * @param $options int A set of options */ - public function __construct($directory) + public function __construct($directory, $options = 0) { $this->directory = $directory; + $this->options = $options; } /** @@ -73,6 +78,15 @@ class Twig_Cache_Filesystem implements Twig_CacheInterface if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $key)) { @chmod($key, 0666 & ~umask()); + if (self::FORCE_BYTECODE_INVALIDATION == ($this->options & self::FORCE_BYTECODE_INVALIDATION)) { + // Compile cached file into bytecode cache + if (function_exists('opcache_invalidate') && ini_get('opcache.enable')) { + opcache_invalidate($key); + } elseif (function_exists('apc_compile_file') && ini_get('apc.enabled')) { + apc_compile_file($key); + } + } + return; }