added an option to force PHP bytecode invalidation when writing a compiled template into the cache

This commit is contained in:
Fabien Potencier
2015-09-13 12:11:42 +02:00
parent e9b08026b3
commit 96a54b3f40
3 changed files with 18 additions and 21 deletions
+1
View File
@@ -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(),
+2 -20
View File
@@ -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),
// ...
));
+15 -1
View File
@@ -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;
}