1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AbterPhp\Framework\Template; |
6
|
|
|
|
7
|
|
|
use Opulence\Cache\ICacheBridge; |
8
|
|
|
|
9
|
|
|
class CacheManager |
10
|
|
|
{ |
11
|
|
|
public const CACHE_KEY_TEMPLATES = 'templates_%s'; |
12
|
|
|
public const CACHE_KEY_DOCUMENT = 'document_%s'; |
13
|
|
|
|
14
|
|
|
protected ICacheBridge $cacheBridge; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Cache constructor. |
18
|
|
|
* |
19
|
|
|
* @param ICacheBridge $cacheBridge |
20
|
|
|
*/ |
21
|
|
|
public function __construct(ICacheBridge $cacheBridge) |
22
|
|
|
{ |
23
|
|
|
$this->cacheBridge = $cacheBridge; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $cacheId |
28
|
|
|
* |
29
|
|
|
* @return CacheData|null |
30
|
|
|
*/ |
31
|
|
|
public function getCacheData(string $cacheId): ?CacheData |
32
|
|
|
{ |
33
|
|
|
$key = $this->getCacheKey($cacheId); |
34
|
|
|
try { |
35
|
|
|
$payload = $this->cacheBridge->get($key); |
36
|
|
|
} catch (\Exception $e) { |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (empty($payload) || !is_string($payload)) { |
41
|
|
|
return null; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$data = json_decode($payload, true); |
45
|
|
|
if (!is_array($data)) { |
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$cacheData = new CacheData(); |
50
|
|
|
|
51
|
|
|
if (array_key_exists(CacheData::PAYLOAD_KEY_DATE, $data)) { |
52
|
|
|
$cacheData->setDate($data[CacheData::PAYLOAD_KEY_DATE]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (array_key_exists(CacheData::PAYLOAD_KEY_SUBTEMPLATES, $data)) { |
56
|
|
|
$cacheData->setSubTemplates($data[CacheData::PAYLOAD_KEY_SUBTEMPLATES]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $cacheData; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $cacheId |
64
|
|
|
* @param array $blocks |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function storeCacheData(string $cacheId, array $blocks): bool |
69
|
|
|
{ |
70
|
|
|
$cacheData = (new CacheData())->setSubTemplates($blocks); |
71
|
|
|
|
72
|
|
|
$payload = json_encode( |
73
|
|
|
[ |
74
|
|
|
CacheData::PAYLOAD_KEY_DATE => $cacheData->getDate(), |
75
|
|
|
CacheData::PAYLOAD_KEY_SUBTEMPLATES => $cacheData->getSubTemplates(), |
76
|
|
|
] |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$key = $this->getCacheKey($cacheId); |
80
|
|
|
|
81
|
|
|
$this->cacheBridge->set($key, $payload, PHP_INT_MAX); |
82
|
|
|
|
83
|
|
|
return $this->cacheBridge->has($key); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $cacheId |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getDocument(string $cacheId): string |
92
|
|
|
{ |
93
|
|
|
$key = $this->getDocumentCacheKey($cacheId); |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
return (string)$this->cacheBridge->get($key); |
97
|
|
|
} catch (\Exception $e) { |
98
|
|
|
return ''; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $cacheId |
104
|
|
|
* @param string $payload |
105
|
|
|
* |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public function storeDocument(string $cacheId, string $payload): bool |
109
|
|
|
{ |
110
|
|
|
$key = $this->getDocumentCacheKey($cacheId); |
111
|
|
|
|
112
|
|
|
$this->cacheBridge->set($key, $payload, PHP_INT_MAX); |
113
|
|
|
|
114
|
|
|
return $this->cacheBridge->has($key); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function flush(): void |
118
|
|
|
{ |
119
|
|
|
$this->cacheBridge->flush(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $cacheId |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
private function getCacheKey(string $cacheId): string |
128
|
|
|
{ |
129
|
|
|
return sprintf(static::CACHE_KEY_TEMPLATES, $cacheId); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $cacheId |
134
|
|
|
* |
135
|
|
|
* @return string |
136
|
|
|
*/ |
137
|
|
|
private function getDocumentCacheKey(string $cacheId): string |
138
|
|
|
{ |
139
|
|
|
return sprintf(static::CACHE_KEY_DOCUMENT, $cacheId); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|