| @@ 8-60 (lines=53) @@ | ||
| 5 | use Doctrine\Common\Cache\Cache; |
|
| 6 | use Kevinrob\GuzzleCache\CacheEntry; |
|
| 7 | ||
| 8 | class DoctrineCacheStorage implements CacheStorageInterface |
|
| 9 | { |
|
| 10 | /** |
|
| 11 | * @var Cache |
|
| 12 | */ |
|
| 13 | protected $cache; |
|
| 14 | ||
| 15 | /** |
|
| 16 | * @param Cache $cache |
|
| 17 | */ |
|
| 18 | public function __construct(Cache $cache) |
|
| 19 | { |
|
| 20 | $this->cache = $cache; |
|
| 21 | } |
|
| 22 | ||
| 23 | /** |
|
| 24 | * {@inheritdoc} |
|
| 25 | */ |
|
| 26 | public function fetch($key) |
|
| 27 | { |
|
| 28 | try { |
|
| 29 | $cache = unserialize($this->cache->fetch($key)); |
|
| 30 | if ($cache instanceof CacheEntry) { |
|
| 31 | return $cache; |
|
| 32 | } |
|
| 33 | } catch (\Exception $ignored) { |
|
| 34 | return; |
|
| 35 | } |
|
| 36 | ||
| 37 | return; |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * {@inheritdoc} |
|
| 42 | */ |
|
| 43 | public function save($key, CacheEntry $data) |
|
| 44 | { |
|
| 45 | try { |
|
| 46 | $lifeTime = $data->getTTL(); |
|
| 47 | if ($lifeTime >= 0) { |
|
| 48 | return $this->cache->save( |
|
| 49 | $key, |
|
| 50 | serialize($data), |
|
| 51 | $lifeTime |
|
| 52 | ); |
|
| 53 | } |
|
| 54 | } catch (\Exception $ignored) { |
|
| 55 | // No fail if we can't save it the storage |
|
| 56 | } |
|
| 57 | ||
| 58 | return false; |
|
| 59 | } |
|
| 60 | } |
|
| 61 | ||
| @@ 8-60 (lines=53) @@ | ||
| 5 | use Illuminate\Cache\Repository as Cache; |
|
| 6 | use Kevinrob\GuzzleCache\CacheEntry; |
|
| 7 | ||
| 8 | class LaravelCacheStorage implements CacheStorageInterface |
|
| 9 | { |
|
| 10 | /** |
|
| 11 | * @var Cache |
|
| 12 | */ |
|
| 13 | protected $cache; |
|
| 14 | ||
| 15 | /** |
|
| 16 | * @param Cache $cache |
|
| 17 | */ |
|
| 18 | public function __construct(Cache $cache) |
|
| 19 | { |
|
| 20 | $this->cache = $cache; |
|
| 21 | } |
|
| 22 | ||
| 23 | /** |
|
| 24 | * {@inheritdoc} |
|
| 25 | */ |
|
| 26 | public function fetch($key) |
|
| 27 | { |
|
| 28 | try { |
|
| 29 | $cache = unserialize($this->cache->get($key)); |
|
| 30 | if ($cache instanceof CacheEntry) { |
|
| 31 | return $cache; |
|
| 32 | } |
|
| 33 | } catch (\Exception $ignored) { |
|
| 34 | return; |
|
| 35 | } |
|
| 36 | ||
| 37 | return; |
|
| 38 | } |
|
| 39 | ||
| 40 | /** |
|
| 41 | * {@inheritdoc} |
|
| 42 | */ |
|
| 43 | public function save($key, CacheEntry $data) |
|
| 44 | { |
|
| 45 | try { |
|
| 46 | $lifeTime = $data->getTTL(); |
|
| 47 | if ($lifeTime >= 0) { |
|
| 48 | return $this->cache->add( |
|
| 49 | $key, |
|
| 50 | serialize($data), |
|
| 51 | $lifeTime |
|
| 52 | ); |
|
| 53 | } |
|
| 54 | } catch (\Exception $ignored) { |
|
| 55 | // No fail if we can't save it the storage |
|
| 56 | } |
|
| 57 | ||
| 58 | return false; |
|
| 59 | } |
|
| 60 | } |
|
| 61 | ||