| 1 | <?php |
||
| 17 | class RedisCache implements Cache |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var Redis |
||
| 21 | */ |
||
| 22 | private $client; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * RedisCache constructor. |
||
| 26 | * |
||
| 27 | * @param Redis $client |
||
| 28 | */ |
||
| 29 | public function __construct(Redis $client) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | */ |
||
| 37 | public function set($key, $value, $timeToLive = 0) |
||
| 38 | { |
||
| 39 | if ($timeToLive > 0) { |
||
| 40 | $this->client->setex($key, $timeToLive, $value); |
||
| 41 | } else { |
||
| 42 | $this->client->set($key, $value); |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritdoc} |
||
| 48 | */ |
||
| 49 | public function has($key) |
||
| 50 | { |
||
| 51 | return $this->client->exists($key); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | public function demand($key) |
||
| 58 | { |
||
| 59 | $value = $this->client->get($key); |
||
| 60 | |||
| 61 | if (!$value && !$this->client->exists($key)) { |
||
| 62 | throw new NotFoundException($key); |
||
| 63 | } |
||
| 64 | |||
| 65 | return $value; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public function get($key, $default = null) |
||
| 72 | { |
||
| 73 | try { |
||
| 74 | return $this->demand($key); |
||
| 75 | } catch (NotFoundException $exception) { |
||
| 76 | return $default; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * {@inheritdoc} |
||
| 82 | */ |
||
| 83 | public function delete($key) |
||
| 84 | { |
||
| 85 | $this->client->delete($key); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | */ |
||
| 91 | public function flush() |
||
| 95 | } |
||
| 96 |