| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services; |
| 4 |
|
| 5 |
use FluentCart\App\App; |
| 6 |
|
| 7 |
class Cache |
| 8 |
{ |
| 9 |
|
| 10 |
public static function get($key, $callback = false, $expire = 3600) |
| 11 |
{ |
| 12 |
$key = static::getKey($key); |
| 13 |
|
| 14 |
$value = wp_cache_get($key, static::getGroupName()); |
| 15 |
|
| 16 |
if ($value !== false) { |
| 17 |
return $value; |
| 18 |
} |
| 19 |
|
| 20 |
if ($callback) { |
| 21 |
$value = $callback(); |
| 22 |
if ($value !== null) { |
| 23 |
static::set($key, $value, $expire); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
return $value; |
| 28 |
} |
| 29 |
|
| 30 |
public static function forget($key): bool |
| 31 |
{ |
| 32 |
return wp_cache_delete(static::getKey($key), static::getGroupName()); |
| 33 |
} |
| 34 |
|
| 35 |
public static function set($key, $value, $expire = 3600): bool |
| 36 |
{ |
| 37 |
return wp_cache_set(static::getKey($key), $value, static::getGroupName(), $expire); |
| 38 |
} |
| 39 |
|
| 40 |
public static function update($key, $value, $expire = 3600): bool |
| 41 |
{ |
| 42 |
return static::set(static::getKey($key), $value, $expire); |
| 43 |
} |
| 44 |
|
| 45 |
private static function getKey(string $key): string |
| 46 |
{ |
| 47 |
return static::getKeyPrefix() . '_' . $key; |
| 48 |
} |
| 49 |
|
| 50 |
private static function getKeyPrefix() |
| 51 |
{ |
| 52 |
return App::config()->get('slug'); |
| 53 |
} |
| 54 |
|
| 55 |
private static function getGroupName(): string |
| 56 |
{ |
| 57 |
return 'fluent-cart'; |
| 58 |
} |
| 59 |
} |