-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCacheFactory.php
More file actions
90 lines (75 loc) · 3.24 KB
/
CacheFactory.php
File metadata and controls
90 lines (75 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
declare(strict_types=1);
namespace Flowpack\Neos\Debug\Cache;
use Flowpack\Neos\Debug\DataCollector\CacheAccessCollector;
use Neos\Cache\Backend\BackendInterface;
use Neos\Cache\EnvironmentConfiguration;
use Neos\Cache\Frontend\FrontendInterface;
use Neos\Flow\Annotations as Flow;
#[Flow\Scope("singleton")]
class CacheFactory extends \Neos\Flow\Cache\CacheFactory
{
protected array $trackedBackends = [];
public function create(
string $cacheIdentifier,
string $cacheObjectName,
string $backendObjectName,
array $backendOptions = [],
bool $persistent = false
): FrontendInterface {
$backend = $this->instantiateProxyBackend(
$cacheIdentifier,
$backendObjectName,
$backendOptions,
$this->environmentConfiguration,
$persistent
);
$cache = $this->instantiateCache($cacheIdentifier, $cacheObjectName, $backend);
$backend->setCache($cache);
return $cache;
}
public function instantiateProxyBackend(
string $cacheIdentifier,
string $backendObjectName,
array $backendOptions,
EnvironmentConfiguration $environmentConfiguration,
bool $persistent = false,
): BackendInterface {
$backendObjectNameParts = explode('\\', $backendObjectName);
$namespace = 'Flowpack\Neos\Debug\Cache';
$proxyBackendClassName = 'NeosDebugTracked' . $cacheIdentifier . array_pop($backendObjectNameParts);
$proxyBackendFQDN = $namespace . '\\' . $proxyBackendClassName;
if (!isset($this->trackedBackends[$cacheIdentifier])) {
$collectorName = '\\' . CacheAccessCollector::class;
$this->trackedBackends[$cacheIdentifier] = true;
$trackedBackendDefinition = '
namespace ' . $namespace . ';
class ' . $proxyBackendClassName . ' extends \\' . $backendObjectName . ' {
public function get(string $entryIdentifier): string|bool
{
$result = parent::get($entryIdentifier);
' . $collectorName . '::trackGet("' . $cacheIdentifier . '", $entryIdentifier, (bool)$result);
return $result;
}
public function set(string $entryIdentifier, string $data, array $tags = [], int $lifetime = null): void
{
$result = parent::set($entryIdentifier, $data, $tags, $lifetime);
' . $collectorName . '::trackSet("' . $cacheIdentifier . '", $entryIdentifier);
}
}
';
eval($trackedBackendDefinition);
}
CacheAccessCollector::registerCache($cacheIdentifier, $backendObjectName);
try {
return $this->instantiateBackend(
$proxyBackendFQDN,
$backendOptions,
$environmentConfiguration,
$persistent
);
} catch (\Throwable $throwable) {
throw new \RuntimeException(sprintf('Cannot instantiate debug proxied backend "%s" for cache "%s"', $backendObjectName, $cacheIdentifier), 1773330049, $throwable);
}
}
}