Last active
January 3, 2023 14:36
-
-
Save marcus-at-localhost/eb2cecb3667dbfc9da9a7890d35445b5 to your computer and use it in GitHub Desktop.
[Guzzle Cache Middleware - Kirby CMS Cache Driver] #kirby #cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Kevinrob\GuzzleCache\Storage; | |
| use Kevinrob\GuzzleCache\CacheEntry; | |
| class KirbyCacheStorage implements CacheStorageInterface | |
| { | |
| /** | |
| * @var object | |
| */ | |
| private $cache; | |
| /** | |
| * @param object $cache | |
| */ | |
| public function __construct($cache) | |
| { | |
| $this->cache = $cache; | |
| } | |
| /** | |
| * @param string $key | |
| * | |
| * @return CacheEntry|null the data or false | |
| */ | |
| public function fetch($key) | |
| { | |
| try { | |
| $cache = unserialize($this->cache->get($key)); | |
| if ($cache instanceof CacheEntry) { | |
| return $cache; | |
| } | |
| } catch (\Exception $ignored) { | |
| // Don't fail if we can't load it | |
| } | |
| return null; | |
| } | |
| /** | |
| * @param string $key | |
| * @param CacheEntry $data | |
| * | |
| * @return bool | |
| */ | |
| public function save($key, CacheEntry $data) | |
| { | |
| try { | |
| return $this->cache->set($key, serialize($data), $data->getTTL()); | |
| } catch (\Exception $ignored) { | |
| // Don't fail if we can't save it | |
| } | |
| return false; | |
| } | |
| /** | |
| * @param string $key | |
| * | |
| * @return bool | |
| */ | |
| public function delete($key) | |
| { | |
| try { | |
| return $this->cache->set($key); | |
| } catch (\Exception $ignored) { | |
| // Don't fail if we can't delete it | |
| } | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment