1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Controller\Traits\Query; |
13
|
|
|
|
14
|
|
|
use Phalcon\Filter\Exception; |
15
|
|
|
use Phalcon\Filter\Filter; |
16
|
|
|
use Phalcon\Support\Collection; |
17
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractInjectable; |
18
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractParams; |
19
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\Query\AbstractCache; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This trait provides methods for caching data for the query. |
23
|
|
|
*/ |
24
|
|
|
trait Cache |
25
|
|
|
{ |
26
|
|
|
use AbstractCache; |
27
|
|
|
|
28
|
|
|
use AbstractInjectable; |
29
|
|
|
use AbstractParams; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* This variable holds the configuration settings for caching. |
33
|
|
|
* @var Collection|null $cacheConfig |
34
|
|
|
*/ |
35
|
|
|
public ?Collection $cacheConfig; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The cache key used for storing data in the cache. |
39
|
|
|
* @var string|null |
40
|
|
|
*/ |
41
|
|
|
public ?string $cacheKey; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The lifetime of the cache data in seconds. |
45
|
|
|
* @var int|null |
46
|
|
|
*/ |
47
|
|
|
public ?int $cacheLifetime; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Initializes the cache. |
51
|
|
|
* |
52
|
|
|
* This method initializes the cache by setting the cache key and lifetime. |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
* @throws Exception |
56
|
|
|
*/ |
57
|
|
|
public function initializeCacheConfig(): void |
58
|
|
|
{ |
59
|
|
|
$this->initializeCacheKey(); |
60
|
|
|
$this->initializeCacheLifetime(); |
61
|
|
|
|
62
|
|
|
$this->setCacheConfig(new Collection([ |
63
|
|
|
'lifetime' => $this->getCacheLifetime(), |
64
|
|
|
'key' => $this->getCacheKey() |
65
|
|
|
])); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Initializes the cache lifetime. |
70
|
|
|
* |
71
|
|
|
* This method retrieves the 'lifetime' parameter using `getParam()` method, |
72
|
|
|
* applies the 'FILTER_ABSINT' filter to it, and then sets the cache lifetime |
73
|
|
|
* using `setCacheLifetime()` method with the filtered value. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
* @throws Exception |
77
|
|
|
*/ |
78
|
|
|
public function initializeCacheLifetime(): void |
79
|
|
|
{ |
80
|
|
|
$lifetime = $this->getParam('lifetime', [Filter::FILTER_ABSINT]); |
81
|
|
|
$this->setCacheLifetime($lifetime); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Initializes the cache key based on the current parameters and user identity. |
86
|
|
|
* |
87
|
|
|
* This method generates a cache key by concatenating the user identity and a hash of the current parameters. |
88
|
|
|
* The generated cache key is then set as the value of the cache key for the current instance of the object. |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
|
public function initializeCacheKey(): void |
93
|
|
|
{ |
94
|
|
|
$paramsKey = hash('sha512', json_encode($this->getParams())); |
95
|
|
|
$identityKey = $this->identity->getUserId(); |
96
|
|
|
$this->setCacheKey('_' . $identityKey . '-' . $paramsKey . '_'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Sets the cache lifetime. |
101
|
|
|
* |
102
|
|
|
* @param int|null $cacheLifetime The cache lifetime. |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
public function setCacheLifetime(?int $cacheLifetime): void |
106
|
|
|
{ |
107
|
|
|
$this->cacheLifetime = $cacheLifetime; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Retrieves the cache lifetime. |
112
|
|
|
* |
113
|
|
|
* @return int|null The cache lifetime. |
114
|
|
|
*/ |
115
|
|
|
public function getCacheLifetime(): ?int |
116
|
|
|
{ |
117
|
|
|
return $this->cacheLifetime; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Sets the cache key. |
122
|
|
|
* |
123
|
|
|
* @param string|null $cacheKey The cache key. |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function setCacheKey(?string $cacheKey): void |
127
|
|
|
{ |
128
|
|
|
$this->cacheKey = $cacheKey; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Retrieves the cache key. |
133
|
|
|
* |
134
|
|
|
* @return string|null The cache key. |
135
|
|
|
*/ |
136
|
|
|
public function getCacheKey(): ?string |
137
|
|
|
{ |
138
|
|
|
return $this->cacheKey; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the cache config collection for the query. |
143
|
|
|
* |
144
|
|
|
* @param Collection|null $cacheConfig The cache config collection, or null to disable. |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
public function setCacheConfig(?Collection $cacheConfig): void |
148
|
|
|
{ |
149
|
|
|
$this->cacheConfig = $cacheConfig; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Retrieves the cache collection for the query. |
154
|
|
|
* |
155
|
|
|
* @return Collection|null The cache config collection, or null if no cache is set. |
156
|
|
|
*/ |
157
|
|
|
public function getCacheConfig(): ?Collection |
158
|
|
|
{ |
159
|
|
|
return $this->cacheConfig; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|