Add RemovableCacheInterface

This commit is contained in:
Fabien Potencier
2024-09-25 08:07:40 +02:00
parent efc527ea65
commit 786a67b1a9
6 changed files with 55 additions and 3 deletions
+1
View File
@@ -1,5 +1,6 @@
# 3.15.0 (2024-XX-XX)
* Add template cache hot reload
* Allow Twig callable argument names to be free-form (snake-case or camelCase) independently of the PHP callable signature
They were automatically converted to snake-cased before
* Deprecate the `attribute` function; use the `.` notation and wrap the name with parenthesis instead
+10 -1
View File
@@ -19,7 +19,7 @@ namespace Twig\Cache;
*
* @author Quentin Devos <quentin@devos.pm>
*/
final class ChainCache implements CacheInterface
final class ChainCache implements CacheInterface, RemovableCacheInterface
{
/**
* @param iterable<CacheInterface> $caches The ordered list of caches used to store and fetch cached items
@@ -69,6 +69,15 @@ final class ChainCache implements CacheInterface
return 0;
}
public function remove(string $name, string $cls): void
{
foreach ($this->caches as $cache) {
if ($cache instanceof RemovableCacheInterface) {
$cache->remove($name, $cls);
}
}
}
/**
* @return string[]
*/
+9 -1
View File
@@ -16,7 +16,7 @@ namespace Twig\Cache;
*
* @author Andrew Tch <andrew@noop.lv>
*/
class FilesystemCache implements CacheInterface
class FilesystemCache implements CacheInterface, RemovableCacheInterface
{
public const FORCE_BYTECODE_INVALIDATION = 1;
@@ -76,6 +76,14 @@ class FilesystemCache implements CacheInterface
throw new \RuntimeException(\sprintf('Failed to write cache file "%s".', $key));
}
public function remove(string $name, string $cls): void
{
$key = $this->generateKey($name, $cls);
if (!@unlink($key) && file_exists($key)) {
throw new \RuntimeException(\sprintf('Failed to delete cache file "%s".', $key));
}
}
public function getTimestamp(string $key): int
{
if (!is_file($key)) {
+5 -1
View File
@@ -16,7 +16,7 @@ namespace Twig\Cache;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
final class NullCache implements CacheInterface
final class NullCache implements CacheInterface, RemovableCacheInterface
{
public function generateKey(string $name, string $className): string
{
@@ -35,4 +35,8 @@ final class NullCache implements CacheInterface
{
return 0;
}
public function remove(string $name, string $cls): void
{
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Cache;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
interface RemovableCacheInterface
{
public function remove(string $name, string $cls): void;
}
+10
View File
@@ -14,6 +14,7 @@ namespace Twig;
use Twig\Cache\CacheInterface;
use Twig\Cache\FilesystemCache;
use Twig\Cache\NullCache;
use Twig\Cache\RemovableCacheInterface;
use Twig\Error\Error;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
@@ -233,6 +234,15 @@ class Environment
return $this->strictVariables;
}
public function removeCache(string $name): void
{
if ($this->cache instanceof RemovableCacheInterface) {
$this->cache->remove($name, $this->getTemplateClass($name));
} else {
throw new \LogicException(\sprintf('The "%s" cache class does not support removing template cache as it does not implement the "RemovableCacheInterface" interface.', \get_class($this->cache)));
}
}
/**
* Gets the current cache implementation.
*