Skip to content

Commit 8795c13

Browse files
committed
Add tagging and lifetime
1 parent 85d17bf commit 8795c13

7 files changed

Lines changed: 459 additions & 6 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
namespace Flowpack\FullPageCache\Aspects;
3+
4+
use Neos\Flow\Annotations as Flow;
5+
use Neos\Flow\Aop\JoinPointInterface;
6+
7+
/**
8+
* @Flow\Aspect
9+
* @Flow\Scope("singleton")
10+
*/
11+
class ContentCacheAspect
12+
{
13+
private $hadUncachedSegments = false;
14+
15+
private $cacheTags = [];
16+
17+
/**
18+
* @var null|int
19+
*/
20+
private $shortestLifetime = null;
21+
22+
/**
23+
* @Flow\Before("method(Neos\Fusion\Core\Cache\ContentCache->createCacheSegment())")
24+
*/
25+
public function grabCachedSegment(JoinPointInterface $joinPoint)
26+
{
27+
$tags = $joinPoint->getMethodArgument('tags');
28+
$lifetime = $joinPoint->getMethodArgument('lifetime');
29+
30+
foreach ($tags as $tag) {
31+
$this->cacheTags[$tag] = true;
32+
}
33+
34+
if ($lifetime !== null && $lifetime < $this->shortestLifetime) {
35+
$this->shortestLifetime = $lifetime;
36+
}
37+
}
38+
39+
/**
40+
* @Flow\Before("method(Neos\Fusion\Core\Cache\ContentCache->createDynamicCachedSegment())")
41+
*/
42+
public function grabDynamicCachedSegment(JoinPointInterface $joinPoint)
43+
{
44+
$tags = $joinPoint->getMethodArgument('tags');
45+
$lifetime = $joinPoint->getMethodArgument('lifetime');
46+
47+
foreach ($tags as $tag) {
48+
$this->cacheTags[$tag] = true;
49+
}
50+
51+
if ($lifetime === null) {
52+
return;
53+
}
54+
55+
if ($this->shortestLifetime === null) {
56+
$this->shortestLifetime = $lifetime;
57+
return;
58+
}
59+
60+
if ($lifetime < $this->shortestLifetime) {
61+
$this->shortestLifetime = $lifetime;
62+
}
63+
}
64+
65+
/**
66+
* @Flow\Before("method(Neos\Fusion\Core\Cache\ContentCache->createUncachedSegment())")
67+
*/
68+
public function grabUncachedSegment(JoinPointInterface $joinPoint)
69+
{
70+
$this->hadUncachedSegments = true;
71+
}
72+
73+
/**
74+
* @return array
75+
*/
76+
public function getAllCacheTags(): array
77+
{
78+
return $this->sanitizeTags(array_keys($this->cacheTags));
79+
}
80+
81+
/**
82+
* @return int|null
83+
*/
84+
public function getShortestLifetime(): ?int
85+
{
86+
return $this->shortestLifetime;
87+
}
88+
89+
/**
90+
* @return bool
91+
*/
92+
public function hasUncachedSegments(): bool
93+
{
94+
return $this->hadUncachedSegments;
95+
}
96+
97+
/**
98+
* Sanitizes the given tag for use with the cache framework
99+
*
100+
* @param string $tag A tag which possibly contains non-allowed characters, for example "NodeType_Acme.Com:Page"
101+
* @return string A cleaned up tag, for example "NodeType_Acme_Com-Page"
102+
*/
103+
protected function sanitizeTag($tag)
104+
{
105+
return strtr($tag, '.:', '_-');
106+
}
107+
108+
/**
109+
* Sanitizes multiple tags with sanitizeTag()
110+
*
111+
* @param array $tags Multiple tags
112+
* @return array The sanitized tags
113+
*/
114+
protected function sanitizeTags(array $tags)
115+
{
116+
foreach ($tags as $key => $value) {
117+
$tags[$key] = $this->sanitizeTag($value);
118+
}
119+
120+
return $tags;
121+
}
122+
}

0 commit comments

Comments
 (0)