|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Cache main file. |
|
4
|
|
|
* |
|
5
|
|
|
* @package App |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright YetiForce Sp. z o.o |
|
8
|
|
|
* @license YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com) |
|
9
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace App; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Cache main class. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Cache |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* The prefix for cache keys. |
|
21
|
|
|
*/ |
|
22
|
|
|
const PREFIX = 'portal'; |
|
23
|
|
|
const LONG = 3600; |
|
24
|
|
|
const MEDIUM = 300; |
|
25
|
|
|
const SHORT = 60; |
|
26
|
|
|
public static $pool; |
|
27
|
|
|
public static $staticPool; |
|
28
|
|
|
/** |
|
29
|
|
|
* Clean the opcache after the script finishes. |
|
30
|
|
|
* |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
public static $clearOpcache = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Initialize cache class. |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function init() |
|
39
|
|
|
{ |
|
40
|
|
|
$driver = \App\Config::get('cachingDriver'); |
|
41
|
|
|
static::$staticPool = new \App\Cache\Base(); |
|
42
|
|
|
if ($driver) { |
|
43
|
|
|
$className = '\App\Cache\\' . $driver; |
|
44
|
|
|
static::$pool = new $className(); |
|
45
|
|
|
|
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
static::$pool = static::$staticPool; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns a Cache Item representing the specified key. |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $key Cache ID |
|
55
|
|
|
* @param mixed $nameSpace |
|
56
|
|
|
* |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
|
|
public static function get($nameSpace, $key) |
|
60
|
|
|
{ |
|
61
|
|
|
return static::$pool->get(static::PREFIX . "{$nameSpace}-{$key}"); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Confirms if the cache contains specified cache item. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $nameSpace |
|
68
|
|
|
* @param string $key Cache ID |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function has($nameSpace, $key): bool |
|
73
|
|
|
{ |
|
74
|
|
|
return static::$pool->has(static::PREFIX . "{$nameSpace}-{$key}"); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Cache Save. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $key Cache ID |
|
81
|
|
|
* @param mixed $value Data to store, supports string, array, objects |
|
82
|
|
|
* @param int $duration Cache TTL (in seconds) |
|
83
|
|
|
* @param mixed $nameSpace |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function save($nameSpace, $key, $value = null, $duration = self::MEDIUM) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
if (!static::$pool->save(static::PREFIX . "{$nameSpace}-{$key}", $value, $duration)) { |
|
90
|
|
|
Log::warning("Error writing to cache. Key: $nameSpace-$key | Value: " . var_export($value, true)); |
|
91
|
|
|
} |
|
92
|
|
|
return $value; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Removes the item from the cache. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $key Cache ID |
|
99
|
|
|
* @param mixed $nameSpace |
|
100
|
|
|
* |
|
101
|
|
|
* @return bool |
|
|
|
|
|
|
102
|
|
|
*/ |
|
103
|
|
|
public static function delete($nameSpace, $key) |
|
104
|
|
|
{ |
|
105
|
|
|
static::$pool->delete(static::PREFIX . "{$nameSpace}-{$key}"); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Deletes all items in the cache. |
|
110
|
|
|
* |
|
111
|
|
|
* @return bool |
|
|
|
|
|
|
112
|
|
|
*/ |
|
113
|
|
|
public static function clear() |
|
114
|
|
|
{ |
|
115
|
|
|
static::$pool->clear(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns a static Cache Item representing the specified key. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $nameSpace |
|
122
|
|
|
* @param string $key Cache ID |
|
123
|
|
|
* |
|
124
|
|
|
* @return mixed |
|
|
|
|
|
|
125
|
|
|
*/ |
|
126
|
|
|
public static function staticGet($nameSpace, $key) |
|
127
|
|
|
{ |
|
128
|
|
|
return static::$staticPool->get(static::PREFIX . "{$nameSpace}-{$key}"); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Confirms if the static cache contains specified cache item. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $nameSpace |
|
135
|
|
|
* @param string $key Cache ID |
|
136
|
|
|
* |
|
137
|
|
|
* @return bool |
|
138
|
|
|
*/ |
|
139
|
|
|
public static function staticHas($nameSpace, $key) |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
|
|
return static::$staticPool->has(static::PREFIX . "{$nameSpace}-{$key}"); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Static cache save. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $nameSpace |
|
148
|
|
|
* @param string $key Cache ID |
|
149
|
|
|
* @param mixed $value Data to store |
|
150
|
|
|
* @param int $duration Cache TTL (in seconds) |
|
|
|
|
|
|
151
|
|
|
* |
|
152
|
|
|
* @return bool |
|
153
|
|
|
*/ |
|
154
|
|
|
public static function staticSave($nameSpace, $key, $value = null) |
|
|
|
|
|
|
155
|
|
|
{ |
|
156
|
|
|
return static::$staticPool->save(static::PREFIX . "{$nameSpace}-{$key}", $value); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Removes the item from the static cache. |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $nameSpace |
|
163
|
|
|
* @param string $key Cache ID |
|
164
|
|
|
* |
|
165
|
|
|
* @return bool |
|
|
|
|
|
|
166
|
|
|
*/ |
|
167
|
|
|
public static function staticDelete($nameSpace, $key) |
|
168
|
|
|
{ |
|
169
|
|
|
static::$staticPool->delete(static::PREFIX . "{$nameSpace}-{$key}"); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Deletes all items in the static cache. |
|
174
|
|
|
* |
|
175
|
|
|
* @return bool |
|
|
|
|
|
|
176
|
|
|
*/ |
|
177
|
|
|
public static function staticClear() |
|
178
|
|
|
{ |
|
179
|
|
|
static::$staticPool->clear(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Clear the opcache after the script finishes. |
|
184
|
|
|
* |
|
185
|
|
|
* @return bool |
|
|
|
|
|
|
186
|
|
|
*/ |
|
187
|
|
|
public static function clearOpcache() |
|
188
|
|
|
{ |
|
189
|
|
|
if (static::$clearOpcache) { |
|
190
|
|
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
register_shutdown_function(function () { |
|
193
|
|
|
static::resetOpcache(); |
|
194
|
|
|
}); |
|
195
|
|
|
static::$clearOpcache = true; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Reset opcache if it is possible. |
|
200
|
|
|
*/ |
|
201
|
|
|
public static function resetOpcache() |
|
202
|
|
|
{ |
|
203
|
|
|
if (\function_exists('opcache_reset')) { |
|
204
|
|
|
\opcache_reset(); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Reset file from opcache if it is possible. |
|
210
|
|
|
* |
|
211
|
|
|
* @param string $path |
|
212
|
|
|
*/ |
|
213
|
|
|
public static function resetFileCache(string $path) |
|
214
|
|
|
{ |
|
215
|
|
|
if (\function_exists('opcache_invalidate')) { |
|
216
|
|
|
\opcache_invalidate($path); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Clear all cache. |
|
222
|
|
|
*/ |
|
223
|
|
|
public static function clearAll() |
|
224
|
|
|
{ |
|
225
|
|
|
static::clearOpcache(); |
|
226
|
|
|
static::clear(); |
|
227
|
|
|
clearstatcache(); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Check whether it's the primary driver. |
|
232
|
|
|
* |
|
233
|
|
|
* @return bool |
|
234
|
|
|
*/ |
|
235
|
|
|
public static function isBase(): bool |
|
236
|
|
|
{ |
|
237
|
|
|
return 'App\Cache\Base' === \get_class(static::$staticPool); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.