| 1: | <?php |
| 2: | /** |
| 3: | * @package OpenCart |
| 4: | * |
| 5: | * @author Daniel Kerr |
| 6: | * @copyright Copyright (c) 2005 - 2022, OpenCart, Ltd. (https://www.opencart.com/) |
| 7: | * @license https://opensource.org/licenses/GPL-3.0 |
| 8: | * |
| 9: | * @see https://www.opencart.com |
| 10: | */ |
| 11: | namespace Opencart\System\Library; |
| 12: | /** |
| 13: | * Class Cache |
| 14: | */ |
| 15: | class Cache { |
| 16: | /** |
| 17: | * @var object |
| 18: | */ |
| 19: | private object $adaptor; |
| 20: | |
| 21: | /** |
| 22: | * Constructor |
| 23: | * |
| 24: | * @param string $adaptor the type of storage for the cache |
| 25: | * @param int $expire Optional parameters |
| 26: | */ |
| 27: | public function __construct(string $adaptor, int $expire = 3600) { |
| 28: | $class = 'Opencart\System\Library\Cache\\' . $adaptor; |
| 29: | |
| 30: | if (class_exists($class)) { |
| 31: | $this->adaptor = new $class($expire); |
| 32: | } else { |
| 33: | throw new \Exception('Error: Could not load cache adaptor ' . $adaptor . ' cache!'); |
| 34: | } |
| 35: | } |
| 36: | |
| 37: | /** |
| 38: | * Get |
| 39: | * |
| 40: | * Gets a cache by key name. |
| 41: | * |
| 42: | * @param string $key The cache key name |
| 43: | * |
| 44: | * @return mixed |
| 45: | */ |
| 46: | public function get(string $key) { |
| 47: | return $this->adaptor->get($key); |
| 48: | } |
| 49: | |
| 50: | /** |
| 51: | * Set |
| 52: | * |
| 53: | * Sets a cache by key value. |
| 54: | * |
| 55: | * @param string $key The cache key |
| 56: | * @param mixed $value The cache value |
| 57: | * @param int $expire The cache expiry |
| 58: | * |
| 59: | * @return void |
| 60: | */ |
| 61: | public function set(string $key, $value, int $expire = 0): void { |
| 62: | $this->adaptor->set($key, $value, $expire); |
| 63: | } |
| 64: | |
| 65: | /** |
| 66: | * Delete |
| 67: | * |
| 68: | * Deletes a cache by key name. |
| 69: | * |
| 70: | * @param string $key The cache key |
| 71: | * |
| 72: | * @return void |
| 73: | */ |
| 74: | public function delete(string $key): void { |
| 75: | $this->adaptor->delete($key); |
| 76: | } |
| 77: | } |
| 78: |