| 1: | <?php |
| 2: | namespace Opencart\System\Library\Cache; |
| 3: | /** |
| 4: | * Class APCU |
| 5: | * |
| 6: | * @package Opencart\System\Library\Cache |
| 7: | */ |
| 8: | class Apcu { |
| 9: | /** |
| 10: | * @var int |
| 11: | */ |
| 12: | private int $expire; |
| 13: | /** |
| 14: | * @var bool |
| 15: | */ |
| 16: | private bool $active; |
| 17: | |
| 18: | /** |
| 19: | * Constructor |
| 20: | * |
| 21: | * @param int $expire |
| 22: | */ |
| 23: | public function __construct(int $expire = 3600) { |
| 24: | $this->expire = $expire; |
| 25: | $this->active = function_exists('apcu_cache_info') && ini_get('apc.enabled'); |
| 26: | } |
| 27: | |
| 28: | /** |
| 29: | * Get |
| 30: | * |
| 31: | * @param string $key |
| 32: | * |
| 33: | * @return mixed |
| 34: | */ |
| 35: | public function get(string $key) { |
| 36: | return $this->active ? apcu_fetch(CACHE_PREFIX . $key) : []; |
| 37: | } |
| 38: | |
| 39: | /** |
| 40: | * Set |
| 41: | * |
| 42: | * @param string $key |
| 43: | * @param mixed $value |
| 44: | * @param int $expire |
| 45: | * |
| 46: | * @return void |
| 47: | */ |
| 48: | public function set(string $key, $value, int $expire = 0): void { |
| 49: | if (!$expire) { |
| 50: | $expire = $this->expire; |
| 51: | } |
| 52: | |
| 53: | if ($this->active) { |
| 54: | apcu_store(CACHE_PREFIX . $key, $value, $expire); |
| 55: | } |
| 56: | } |
| 57: | |
| 58: | /** |
| 59: | * Delete |
| 60: | * |
| 61: | * @param string $key |
| 62: | * |
| 63: | * @return void |
| 64: | */ |
| 65: | public function delete(string $key): void { |
| 66: | if ($this->active) { |
| 67: | $cache_info = apcu_cache_info(); |
| 68: | |
| 69: | $cache_list = $cache_info['cache_list']; |
| 70: | |
| 71: | foreach ($cache_list as $entry) { |
| 72: | if (strpos($entry['info'], CACHE_PREFIX . $key) === 0) { |
| 73: | apcu_delete($entry['info']); |
| 74: | } |
| 75: | } |
| 76: | } |
| 77: | } |
| 78: | |
| 79: | /** |
| 80: | * Delete all cache |
| 81: | * |
| 82: | * @return bool |
| 83: | */ |
| 84: | public function flush(): bool { |
| 85: | $status = false; |
| 86: | |
| 87: | if (function_exists('apcu_clear_cache')) { |
| 88: | $status = apcu_clear_cache(); |
| 89: | } |
| 90: | |
| 91: | return $status; |
| 92: | } |
| 93: | } |
| 94: |