Removed need for unique ID

This commit is contained in:
Andy Miller
2019-10-13 17:02:07 -06:00
parent e59c596886
commit d368aeafd4
2 changed files with 12 additions and 14 deletions

View File

@@ -17,15 +17,15 @@ use Twig\Node\Node;
class TwigNodeCache extends Node
{
/**
* @param AbstractExpression $key
* @param AbstractExpression $lifetime
* @param String $key
* @param Int $lifetime
* @param Node $body
* @param integer $lineno
* @param string $tag
*/
public function __construct(AbstractExpression $key, AbstractExpression $lifetime, Node $body, $lineno, $tag = null)
public function __construct(String $key, int $lifetime, Node $body, $lineno, $tag = null)
{
parent::__construct(array('key' => $key, 'body' => $body, 'lifetime' => $lifetime), array(), $lineno, $tag);
parent::__construct(array('body' => $body), array( 'key' => $key, 'lifetime' => $lifetime), $lineno, $tag);
}
/**
@@ -33,16 +33,12 @@ class TwigNodeCache extends Node
*/
public function compile(Compiler $compiler)
{
$boo = $this->getAttribute('key');
$compiler
->addDebugInfo($this)
->write("\$cache = \\Grav\\Common\\Grav::instance()['cache'];\n")
->write("\$key = \"twigcache-\" . ")
->subcompile($this->getNode('key'))
->write(";\n")
->write("\$lifetime = ")
->subcompile($this->getNode('lifetime'))
->write(";\n")
->write("\$key = \"twigcache-\" . \"" . $this->getAttribute('key') . "\";\n")
->write("\$lifetime = " . $this->getAttribute('lifetime') . ";\n")
->write("\$cache_body = \$cache->fetch(\$key);\n")
->write("if (\$cache_body === false) {\n")
->indent()

View File

@@ -16,9 +16,11 @@ use Twig\TokenParser\AbstractTokenParser;
/**
* Adds ability to cache Twig between tags.
*
* {% cache 'unique-key' 600 %}
* {% cache 600 %}
* {{ some_complex_work() }}
* {% endcache %}
*
* Where the `600` is an optional lifetime in seconds
*/
class TwigTokenParserCache extends AbstractTokenParser
{
@@ -29,8 +31,8 @@ class TwigTokenParserCache extends AbstractTokenParser
{
$lineno = $token->getLine();
$stream = $this->parser->getStream();
$key = $this->parser->getExpressionParser()->parseExpression();
$lifetime =$this->parser->getExpressionParser()->parseExpression();
$key = $this->parser->getVarName() . $lineno;
$lifetime = $this->parser->getExpressionParser()->parseExpression()->getAttribute('value');
$stream->expect(Token::BLOCK_END_TYPE);
$body = $this->parser->subparse(array($this, 'decideCacheEnd'), true);