Cache tag: key is no longer required + lifetime optional

This commit is contained in:
Andy Miller
2019-10-13 17:10:41 -06:00
parent d368aeafd4
commit d0800136b7
3 changed files with 15 additions and 7 deletions

View File

@@ -8,6 +8,7 @@
1. [](#improved)
* Improved `Flex Users`: obey blueprints and allow Flex to be used in admin only
* Improved `Flex` to support custom site template paths
* Changed Twig `{% cache %}` tag to not need unique key, and `lifetime` is now optional
1. [](#bugfix)
* Fixed `Page::untranslatedLanguages()` not being symmetrical to `Page::translatedLanguages()`
* Fixed `Flex Pages` not calling `onPageProcessed` event when cached

View File

@@ -17,13 +17,13 @@ use Twig\Node\Node;
class TwigNodeCache extends Node
{
/**
* @param String $key
* @param Int $lifetime
* @param Node $body
* @param integer $lineno
* @param string $tag
* @param string $key unique name for key
* @param int $lifetime in seconds
* @param Node $body
* @param integer $lineno
* @param string $tag
*/
public function __construct(String $key, int $lifetime, Node $body, $lineno, $tag = null)
public function __construct(string $key, int $lifetime, Node $body, $lineno, $tag = null)
{
parent::__construct(array('body' => $body), array( 'key' => $key, 'lifetime' => $lifetime), $lineno, $tag);
}

View File

@@ -9,6 +9,7 @@
namespace Grav\Common\Twig\TokenParser;
use Grav\Common\Grav;
use Grav\Common\Twig\Node\TwigNodeCache;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;
@@ -32,7 +33,13 @@ class TwigTokenParserCache extends AbstractTokenParser
$lineno = $token->getLine();
$stream = $this->parser->getStream();
$key = $this->parser->getVarName() . $lineno;
$lifetime = $this->parser->getExpressionParser()->parseExpression()->getAttribute('value');
$lifetime = Grav::instance()['cache']->getLifetime();
// Check for optional lifetime override
if (!$stream->test(Token::BLOCK_END_TYPE)) {
$lifetime_expr = $this->parser->getExpressionParser()->parseExpression();
$lifetime = $lifetime_expr->getAttribute('value');
}
$stream->expect(Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideCacheEnd'), true);