PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.94
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.94
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Cache / Cache.php

Cache.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.94, at vendor/wpfluent/framework/src/WPFluent/Cache/Cache.php

367 lines 8.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Framework\Cache;
4
5 use Closure;
6 use Throwable;
7 use RuntimeException;
8 use FluentCommunity\Framework\Support\Path;
9 use FluentCommunity\Framework\Foundation\App;
10
11 class Cache
12 {
13 protected static $cacheDir = null;
14
15 /**
16 * Initialize the cache directory.
17 */
18 public static function init($cacheDir = null)
19 {
20 if (static::isPersistent()) return;
21
22 static::$cacheDir = static::getDir($cacheDir);
23
24 if (!is_dir(static::$cacheDir) && !mkdir(static::$cacheDir, 0755, true)) {
25 throw new RuntimeException(
26 "Unable to create cache directory: " . static::$cacheDir
27 );
28 }
29
30 return new static;
31 }
32
33 /**
34 * Resolve cache directory.
35 *
36 * @param string|null $cacheDir
37 * @return string
38 */
39 public static function getDir($cacheDir = null)
40 {
41 $slug = App::config()->get('app.slug');
42
43 $cacheDir ??= WP_CONTENT_DIR . "/uploads/{$slug}/storage/app/cache";
44
45 return rtrim($cacheDir, '/');
46 }
47
48 /**
49 * Store data in the cache using transients.
50 *
51 * @param string $key
52 * @param mixed $value
53 * @param int $ttl Time to live (in seconds)
54 * @return bool
55 */
56 public static function put($key, $value, $ttl = HOUR_IN_SECONDS)
57 {
58 $key = static::key($key);
59
60 if (static::isPersistent()) {
61 return wp_cache_set($key, $value, null, $ttl);
62 } else {
63 return static::filePut($key, $value, $ttl);
64 }
65 }
66
67 /**
68 * Store data in the cache using transients.
69 *
70 * @param string $key
71 * @param mixed $value
72 * @param int $ttl Time to live (in seconds)
73 * @return bool
74 */
75 public static function set($key, $value, $ttl = HOUR_IN_SECONDS)
76 {
77 return static::put($key, $value, $ttl);
78 }
79
80 /**
81 * Retrieve data from the cache.
82 *
83 * @param string $key
84 * @param mixed $default
85 * @return mixed
86 */
87 public static function get($key)
88 {
89 $key = static::key($key);
90
91 if (static::isPersistent()) {
92 $value = wp_cache_get($key, null, false, $found);
93 $value = $found ? $value : null;
94 } else {
95 $value = static::fileGet($key) ?: null;
96 }
97
98 return $value;
99 }
100
101 /**
102 * Remove data from the cache.
103 *
104 * @param string $key
105 * @return bool
106 */
107 public static function forget($key)
108 {
109 $key = static::key($key);
110
111 if (static::isPersistent()) {
112 return wp_cache_delete($key, static::$cacheGroup);
113 }
114
115 return static::fileForget($key);
116 }
117
118 /**
119 * Store data in the cache "forever" using 5 years of expiry time.
120 *
121 * @param string $key
122 * @param mixed $value
123 * @return bool
124 */
125 public static function forever($key, $value)
126 {
127 $key = static::key($key);
128
129 if (static::isPersistent()) {
130 return wp_cache_set($key, $value, null, 0);
131 } else {
132 return static::filePut($key, $value, 0);
133 }
134 }
135
136 /**
137 * Get an item from the cache, or store the default value if not present.
138 *
139 * @param string $key
140 * @param \Closure $callback
141 * @param int $ttl Time to live (in seconds)
142 * @return mixed
143 */
144 public static function remember(
145 $key, Closure $callback, $ttl = HOUR_IN_SECONDS
146 )
147 {
148 $value = static::get($key);
149
150 if ($value !== null) {
151 return $value;
152 }
153
154 try {
155 if (($value = $callback()) && !is_wp_error($value)) {
156 static::set($key, $value, $ttl);
157
158 return $value;
159 }
160
161 } catch (Throwable $e) {
162 error_log("Cache callback error [{$key}]: " . $e->getMessage());
163 }
164 }
165
166 /**
167 * Increment the value of an item in the cache.
168 *
169 * @param string $key
170 * @param integer $count
171 * @return bool
172 */
173 public static function increment($key, $count = 1)
174 {
175 $value = static::get($key);
176
177 if (is_numeric($value)) {
178 return static::set($key, $value + $count);
179 }
180
181 return false;
182 }
183
184 /**
185 * Decrement the value of an item in the cache.
186 *
187 * @param string $key
188 * @param integer $count
189 * @return bool
190 */
191 public static function decrement($key, $count = 1)
192 {
193 $value = static::get($key);
194
195 if (is_numeric($value)) {
196 return static::set($key, $value - $count);
197 }
198
199 return false;
200 }
201
202 /**
203 * Delete all cached items for the plugin.
204 *
205 * @return void
206 */
207 public static function flush()
208 {
209 $key = App::config()->get('app.slug') . '_cache_';
210
211 if (static::isPersistent()) {
212
213 global $wp_object_cache;
214
215 $pattern = '~^'. preg_quote($key, '~') . '~i';
216
217 $keys = preg_grep($pattern, array_keys($wp_object_cache->cache));
218
219 foreach ($keys as $key) {
220 unset($wp_object_cache->cache[$key]);
221 }
222
223 return true;
224 }
225
226 return static::fileFlush();
227 }
228
229 /**
230 * Check if a key exists in the cache.
231 *
232 * @param string $key
233 * @return bool
234 */
235 public static function has($key)
236 {
237 return static::get($key) !== null;
238 }
239
240 /**
241 * Make unique key with config.app.slug
242 *
243 * @param string $key
244 * @return string
245 */
246 protected static function key($key)
247 {
248 $slug = App::config()->get('app.slug');
249
250 return $slug . '_cache_' . sanitize_key($key);
251 }
252
253 /**
254 * Check if persistent cache is available.
255 *
256 * @return bool
257 */
258 protected static function isPersistent()
259 {
260 return wp_using_ext_object_cache();
261 }
262
263 /**
264 * Store data in a file if no persistent cache is available.
265 *
266 * @param string $key
267 * @param mixed $data
268 * @param int $expiration
269 */
270 protected static function filePut($key, $data, $expiration)
271 {
272 try {
273 return static::store($key, [
274 'data' => $data,
275 'expiration' => time() + $expiration,
276 ]);
277 } catch (Throwable $e) {
278 error_log(__METHOD__ .' - '. $e->getMessage());
279 throw $e;
280 }
281
282 }
283
284 /**
285 * Store the data in a file if no persistent cache is available.
286 *
287 * @param string $key
288 * @param mixed $payload
289 * @return bool
290 */
291 protected static function store($key, $payload)
292 {
293 if (!static::$cacheDir || !is_readable(static::$cacheDir)) {
294 static::init();
295 }
296
297 $data = $payload['data'];
298
299 if (is_object($data) && method_exists($data, 'toArray')) {
300 $payload['data'] = $data->toArray();
301 }
302
303 $filePath = static::$cacheDir . '/' . md5($key) . '.cache';
304
305 return file_put_contents($filePath, serialize($payload)) !== false;
306 }
307
308 /**
309 * Retrieve data from the file cache.
310 *
311 * @param string $key
312 * @return mixed|null
313 */
314 protected static function fileGet($key)
315 {
316 $filePath = static::$cacheDir . '/' . md5($key) . '.cache';
317
318 if (file_exists($filePath)) {
319 $data = unserialize(file_get_contents($filePath));
320 if ($data['expiration'] >= time()) {
321 return $data['data'];
322 }
323 static::fileForget($key);
324 }
325
326 return false;
327 }
328
329 /**
330 * Remove an item from the file cache.
331 *
332 * @param string $key
333 * @return bool
334 */
335 protected static function fileForget($key)
336 {
337 $filePath = static::$cacheDir . '/' . md5($key) . '.cache';
338
339 if (file_exists($filePath)) {
340 unlink($filePath);
341 return true;
342 }
343
344 return false;
345 }
346
347 /**
348 * Flush all file cache entries.
349 *
350 * @return bool
351 */
352 protected static function fileFlush()
353 {
354 if (!is_dir(static::$cacheDir)) {
355 return false;
356 }
357
358 $files = glob(static::$cacheDir . '/*.cache');
359
360 foreach ($files as $file) {
361 @unlink($file);
362 }
363
364 return true;
365 }
366 }
367