Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ab673cc

Browse files
vitkutnydg
authored andcommittedJan 29, 2017
Cache: create dependencies when closing macro (#49)
- prevents invalid expiration from dependencies callback when caching time-expensive data
1 parent 62bf8ec commit ab673cc

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed
 

‎src/Bridges/CacheLatte/CacheMacro.php

+15-7
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public function nodeOpened(Latte\MacroNode $node)
6969
*/
7070
public function nodeClosed(Latte\MacroNode $node)
7171
{
72-
$node->closingCode = '<?php $_tmp = array_pop($this->global->cacheStack); if (!$_tmp instanceof stdClass) $_tmp->end(); } ?>';
72+
$node->closingCode = Latte\PhpWriter::using($node)
73+
->write('<?php Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, %node.array?); } ?>');
7374
}
7475

7576

@@ -112,19 +113,26 @@ public static function createCache(Nette\Caching\IStorage $cacheStorage, $key, &
112113

113114
$cache = new Cache($cacheStorage, 'Nette.Templating.Cache');
114115
if ($helper = $cache->start($key)) {
116+
$parents[] = $helper;
117+
}
118+
return $helper;
119+
}
120+
121+
122+
public static function endCache(& $parents, array $args = NULL)
123+
{
124+
$helper = array_pop($parents);
125+
if ($helper instanceof Nette\Caching\OutputHelper) {
115126
if (isset($args['dependencies'])) {
116127
$args += call_user_func($args['dependencies']);
117128
}
118129
if (isset($args['expire'])) {
119130
$args['expiration'] = $args['expire']; // back compatibility
120131
}
121-
$helper->dependencies = [
122-
$cache::TAGS => isset($args['tags']) ? $args['tags'] : NULL,
123-
$cache::EXPIRATION => isset($args['expiration']) ? $args['expiration'] : '+ 7 days',
124-
];
125-
$parents[] = $helper;
132+
$helper->dependencies[Nette\Caching\Cache::TAGS] = isset($args['tags']) ? $args['tags'] : NULL;
133+
$helper->dependencies[Nette\Caching\Cache::EXPIRATION] = isset($args['expiration']) ? $args['expiration'] : '+ 7 days';
134+
$helper->end();
126135
}
127-
return $helper;
128136
}
129137

130138
}

‎tests/Bridges.Latte/CacheMacro.createCache.phpt

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ require __DIR__ . '/../bootstrap.php';
1515
test(function () {
1616
$parents = [];
1717
$dp = [Cache::TAGS => ['rum', 'cola']];
18-
$outputHelper = CacheMacro::createCache(new DevNullStorage(), 'test', $parents, $dp);
18+
$outputHelper = CacheMacro::createCache(new DevNullStorage(), 'test', $parents);
1919
Assert::type(Nette\Caching\OutputHelper::class, $outputHelper);
20+
CacheMacro::endCache($parents, $dp);
2021
Assert::same($dp + [Cache::EXPIRATION => '+ 7 days'], $outputHelper->dependencies);
2122
});
2223

@@ -26,7 +27,8 @@ test(function () {
2627
$dpFallback = function () use ($dp) {
2728
return $dp;
2829
};
29-
$outputHelper = CacheMacro::createCache(new DevNullStorage(), 'test', $parents, ['dependencies' => $dpFallback]);
30+
$outputHelper = CacheMacro::createCache(new DevNullStorage(), 'test', $parents);
31+
CacheMacro::endCache($parents, ['dependencies' => $dpFallback]);
3032
Assert::same($dp + [Cache::EXPIRATION => '+ 7 days'], $outputHelper->dependencies);
3133
});
3234

@@ -39,6 +41,7 @@ test(function () {
3941
$dpFallback = function () use ($dp) {
4042
return $dp;
4143
};
42-
$outputHelper = CacheMacro::createCache(new DevNullStorage(), 'test', $parents, ['dependencies' => $dpFallback]);
44+
$outputHelper = CacheMacro::createCache(new DevNullStorage(), 'test', $parents);
45+
CacheMacro::endCache($parents, ['dependencies' => $dpFallback]);
4346
Assert::same($dp, $outputHelper->dependencies);
4447
});

‎tests/Bridges.Latte/expected/CacheMacro.cache.inc.phtml

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ class Template%a% extends Latte\Runtime\Template
1111
?> <?php echo %a% ?>
1212

1313
<?php
14-
$_tmp = array_pop($this->global->cacheStack);
15-
if (!$_tmp instanceof stdClass) $_tmp->end();
14+
Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack);
1615
}
1716
%A%
1817
}

‎tests/Bridges.Latte/expected/CacheMacro.cache.phtml

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ Noncached content
2121
?>
2222

2323
<?php
24-
$_tmp = array_pop($this->global->cacheStack);
25-
if (!$_tmp instanceof stdClass) $_tmp->end();
24+
Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, [$id, 'tags' => 'mytag']);
2625
}
2726
%A%
2827
}

0 commit comments

Comments
 (0)
Please sign in to comment.