1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matecat\SimpleS3\Components\Cache; |
4
|
|
|
|
5
|
|
|
use Predis\Client as Redis; |
6
|
|
|
use Matecat\SimpleS3\Helpers\File; |
7
|
|
|
|
8
|
|
|
class RedisCache implements CacheInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $prefixSeparator = DIRECTORY_SEPARATOR; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Redis |
17
|
|
|
*/ |
18
|
|
|
private $redisClient; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* RedisCache constructor. |
22
|
|
|
* |
23
|
|
|
* @param Redis $redisClient |
24
|
|
|
*/ |
25
|
35 |
|
public function __construct(Redis $redisClient) |
26
|
|
|
{ |
27
|
35 |
|
$this->redisClient = $redisClient; |
28
|
35 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public function flushAll() |
34
|
|
|
{ |
35
|
|
|
$flush = $this->redisClient->flushall(); |
36
|
|
|
|
37
|
|
|
if ($flush->getPayload() === 'OK') { |
38
|
|
|
return true; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $bucket |
46
|
|
|
* @param string $keyname |
47
|
|
|
* @param null $version |
|
|
|
|
48
|
|
|
* |
49
|
|
|
* @return array|mixed |
50
|
|
|
*/ |
51
|
11 |
|
public function get($bucket, $keyname, $version = null) |
52
|
|
|
{ |
53
|
11 |
|
if (null != $version) { |
|
|
|
|
54
|
2 |
|
$keyname .= '<VERSION_ID:'.$version.'>'; |
55
|
|
|
} |
56
|
|
|
|
57
|
11 |
|
return unserialize($this->redisClient->hget($this->getHashPrefix($bucket, $keyname), $keyname)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $bucket |
62
|
|
|
* @param string $keyname |
63
|
|
|
* @param null $version |
|
|
|
|
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
12 |
|
public function has($bucket, $keyname, $version = null) |
68
|
|
|
{ |
69
|
12 |
|
if (null != $version) { |
|
|
|
|
70
|
2 |
|
$keyname .= '<VERSION_ID:'.$version.'>'; |
71
|
|
|
} |
72
|
|
|
|
73
|
12 |
|
return (1 === $this->redisClient->hexists($this->getHashPrefix($bucket, $keyname), $keyname)) ? true : false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $bucket |
78
|
|
|
* @param string $keyname |
79
|
|
|
* @param null $version |
|
|
|
|
80
|
|
|
*/ |
81
|
6 |
|
public function remove($bucket, $keyname, $version = null) |
82
|
|
|
{ |
83
|
6 |
|
if (null != $version) { |
|
|
|
|
84
|
1 |
|
$keyname .= '<VERSION_ID:'.$version.'>'; |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
$this->redisClient->hdel($this->getHashPrefix($bucket, $keyname), [$keyname]); |
88
|
6 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $bucket |
92
|
|
|
* @param string $keyname |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
10 |
|
public function search($bucket, $keyname) |
97
|
|
|
{ |
98
|
10 |
|
return $this->redisClient->hkeys($this->getHashPrefix($bucket, $keyname)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $bucket |
103
|
|
|
* @param string $keyname |
104
|
|
|
* @param mixed $content |
105
|
|
|
* @param null $version |
|
|
|
|
106
|
|
|
* @param null $ttl |
|
|
|
|
107
|
|
|
*/ |
108
|
17 |
|
public function set($bucket, $keyname, $content, $version = null, $ttl = null) |
109
|
|
|
{ |
110
|
17 |
|
if (null != $version) { |
|
|
|
|
111
|
3 |
|
$keyname .= '<VERSION_ID:'.$version.'>'; |
112
|
|
|
} |
113
|
|
|
|
114
|
17 |
|
$this->redisClient->hset($this->getHashPrefix($bucket, $keyname), $keyname, serialize($content)); |
115
|
|
|
|
116
|
17 |
|
if ($this->ttl($bucket, $keyname) === -1) { |
117
|
10 |
|
$this->redisClient->expire($this->getHashPrefix($bucket, $keyname), (null != $ttl) ? $ttl * 60 : self::TTL_STANDARD); |
|
|
|
|
118
|
|
|
} |
119
|
17 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $separator |
123
|
|
|
*/ |
124
|
32 |
|
public function setPrefixSeparator($separator) |
125
|
|
|
{ |
126
|
32 |
|
$this->prefixSeparator = $separator; |
127
|
32 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $bucket |
131
|
|
|
* @param string $keyname |
132
|
|
|
* @param null $version |
|
|
|
|
133
|
|
|
* |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
17 |
|
public function ttl($bucket, $keyname, $version = null) |
137
|
|
|
{ |
138
|
17 |
|
if (null != $version) { |
|
|
|
|
139
|
|
|
$keyname .= '<VERSION_ID:'.$version.'>'; |
140
|
|
|
} |
141
|
|
|
|
142
|
17 |
|
return $this->redisClient->ttl($this->getHashPrefix($bucket, $keyname)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param string $bucketName |
147
|
|
|
* @param string $keyName |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
23 |
|
private function getHashPrefix($bucketName, $keyName) |
152
|
|
|
{ |
153
|
23 |
|
return hash(self::HASH_ALGORITHM, $bucketName . self::HASH_SAFE_SEPARATOR . $this->getDirName($keyName)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $item |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
23 |
|
private function getDirName($item) |
162
|
|
|
{ |
163
|
23 |
|
if (File::endsWith($item, $this->prefixSeparator)) { |
164
|
10 |
|
return $item; |
165
|
|
|
} |
166
|
|
|
|
167
|
20 |
|
$fileInfo = File::getPathInfo($item); |
168
|
|
|
|
169
|
20 |
|
return $fileInfo['dirname'] . $this->prefixSeparator; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|