mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 11:27:00 +00:00
rewrote the recipes about APC and opcache to avoid deprecation notices
This commit is contained in:
+37
-10
@@ -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
|
||||||
-------------------------------
|
-------------------------------
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user