rewrote the recipes about APC and opcache to avoid deprecation notices

This commit is contained in:
Fabien Potencier
2015-09-12 11:46:38 +02:00
parent b5c7f5cd5a
commit e9b08026b3
+37 -10
View File
@@ -337,25 +337,52 @@ Refreshing modified Templates when OPcache or APC is enabled
When using OPcache with ``opcache.validate_timestamps`` set to ``0`` or APC 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 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 cache won't update the cache.
``Twig_Environment`` and force the update of the cache when Twig rewrites the
cache::
class Twig_Environment_APC extends Twig_Environment 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
{ {
protected function writeCacheFile($file, $content) public function write($key, $content)
{ {
parent::writeCacheFile($file, $content); parent::write($key, $content);
// Compile cached file into bytecode cache // Compile cached file into bytecode cache
if (extension_loaded('Zend OPcache') && ini_get('opcache.enable')) { if (function_exists('opcache_invalidate') && ini_get('opcache.enable')) {
opcache_invalidate($file); opcache_invalidate($key);
} elseif (extension_loaded('apc') && ini_get('apc.enabled')) { } elseif (function_exists('apc_compile_file') && ini_get('apc.enabled')) {
apc_compile_file($file); apc_compile_file($key);
} }
} }
} }
Then, use that new class as the template cache::
$twig = new Twig_Environment($loader, array(
'cache' => new OpCacheAwareCacheFilesystem('/some/cache/path'),
// ...
));
.. note::
Before Twig 1.22, you should extend ``Twig_Environment`` instead::
class OpCacheAwareTwigEnvironment extends Twig_Environment
{
protected function writeCacheFile($file, $content)
{
parent::writeCacheFile($file, $content);
// 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 Reusing a stateful Node Visitor
------------------------------- -------------------------------