1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmp\Cache\Backend; |
4
|
|
|
|
5
|
|
|
use Cmp\Cache\Cache; |
6
|
|
|
use Cmp\Cache\Exceptions\NotFoundException; |
7
|
|
|
use Cmp\Cache\TimeToLiveAwareCache; |
8
|
|
|
use Cmp\Cache\Traits\MultiCacheTrait; |
9
|
|
|
use Redis; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class RedisCache |
13
|
|
|
* |
14
|
|
|
* A redis powered backend for caching |
15
|
|
|
* |
16
|
|
|
* @package Cmp\Cache\Infrastureture\Backend |
17
|
|
|
*/ |
18
|
|
|
class RedisCache implements Cache |
19
|
|
|
{ |
20
|
|
|
use MultiCacheTrait { |
21
|
|
|
MultiCacheTrait::setItems as setItemsTrait; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
const DEFAULT_HOST = '127.0.0.1'; |
25
|
|
|
const DEFAULT_PORT = 6379; |
26
|
|
|
const DEFAULT_DB = 0; |
27
|
|
|
const DEFAULT_TIMEOUT = 0.0; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Redis |
31
|
|
|
*/ |
32
|
|
|
private $client; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* RedisCache constructor. |
36
|
|
|
* |
37
|
|
|
* @param Redis $client |
38
|
|
|
*/ |
39
|
1 |
|
public function __construct(Redis $client) |
40
|
|
|
{ |
41
|
1 |
|
$this->client = $client; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
1 |
|
public function set($key, $value, $timeToLive = null) |
48
|
|
|
{ |
49
|
1 |
|
if ($timeToLive > 0) { |
50
|
1 |
|
return $this->client->setex($key, $timeToLive, $value); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return $this->client->set($key, $value); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
1 |
|
public function setItems(array $items, $timeToLive = null) |
60
|
|
|
{ |
61
|
1 |
|
if (!$timeToLive) { |
|
|
|
|
62
|
1 |
|
return $this->client->mset($items); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $this->setItemsTrait($items, $timeToLive); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
1 |
|
public function has($key) |
72
|
|
|
{ |
73
|
1 |
|
return $this->client->exists($key); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
1 |
|
public function demand($key) |
80
|
|
|
{ |
81
|
1 |
|
$value = $this->client->get($key); |
|
|
|
|
82
|
|
|
|
83
|
1 |
|
if (!$value) { |
84
|
1 |
|
throw new NotFoundException($key); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $value; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
1 |
|
public function get($key, $default = null) |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
1 |
|
return $this->demand($key); |
97
|
1 |
|
} catch (NotFoundException $exception) { |
98
|
1 |
|
return $default; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function getItems(array $keys) |
106
|
|
|
{ |
107
|
|
|
return array_walk($this->client->mget($keys), function(&$value) { |
|
|
|
|
108
|
|
|
$value = $value === false ? null : $value; |
109
|
|
|
}); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
1 |
|
public function delete($key) |
116
|
|
|
{ |
117
|
1 |
|
return (bool) $this->client->delete($key); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function deleteAll(array $keys) |
124
|
|
|
{ |
125
|
|
|
return (bool) call_user_func_array([$this->client, 'delete', $keys]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
1 |
|
public function flush() |
132
|
|
|
{ |
133
|
1 |
|
return $this->client->flushDB(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
1 |
|
public function getTimeToLive($key) |
140
|
|
|
{ |
141
|
1 |
|
$timeToLive = $this->client->ttl($key); |
|
|
|
|
142
|
|
|
|
143
|
1 |
|
return false === $timeToLive || $timeToLive <= 0 ? null : $timeToLive; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: