feature #1823 rewrote the recipes about APC and opcache to avoid deprecation notices (fabpot)

This PR was merged into the 1.x branch.

Discussion
----------

rewrote the recipes about APC and opcache to avoid deprecation notices

Commits
-------

96a54b3 added an option to force PHP bytecode invalidation when writing a compiled template into the cache
e9b0802 rewrote the recipes about APC and opcache to avoid deprecation notices
This commit is contained in:
Fabien Potencier
2015-09-13 13:17:46 +02:00
3 changed files with 38 additions and 14 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(),
+22 -13
View File
@@ -337,24 +337,33 @@ Refreshing modified Templates when OPcache or APC is enabled
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, one can extend
``Twig_Environment`` and force the update of the cache when Twig rewrites the
cache::
cache won't update the cache.
class Twig_Environment_APC extends Twig_Environment
{
protected function writeCacheFile($file, $content)
To get around this, force Twig to invalidate the bytecode cache::
$twig = new Twig_Environment($loader, array(
'cache' => new Twig_Cache_Filesystem('/some/cache/path', Twig_Cache_Filesystem::FORCE_BYTECODE_INVALIDATION),
// ...
));
.. note::
Before Twig 1.22, you should extend ``Twig_Environment`` instead::
class OpCacheAwareTwigEnvironment extends Twig_Environment
{
parent::writeCacheFile($file, $content);
protected function writeCacheFile($file, $content)
{
parent::writeCacheFile($file, $content);
// Compile cached file into bytecode cache
if (extension_loaded('Zend OPcache') && ini_get('opcache.enable')) {
opcache_invalidate($file);
} elseif (extension_loaded('apc') && ini_get('apc.enabled')) {
apc_compile_file($file);
// Compile cached file into bytecode cache
if (function_exists('opcache_invalidate') && ini_get('opcache.enable')) {
opcache_invalidate($file);
} elseif (function_exists('apc_compile_file') && ini_get('apc.enabled')) {
apc_compile_file($file);
}
}
}
}
Reusing a stateful Node Visitor
-------------------------------
+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;
}