-
-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathRedisSentinelStore.php
More file actions
105 lines (84 loc) · 2.18 KB
/
RedisSentinelStore.php
File metadata and controls
105 lines (84 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace TusPhp\Cache;
use Carbon\Carbon;
use TusPhp\Config;
use Predis\Client as RedisClient;
class RedisSentinelStore extends AbstractCache
{
/** @var RedisClient */
protected $redis;
/**
* RedisStore constructor.
*
* @param array $options
*/
public function __construct(array $params = [], array $options = [])
{
$options = empty($options) ? Config::get('sentinel.options') : $options;
$params = empty($params) ? Config::get('sentinel.params') : $params;
$this->redis = new RedisClient($params, $options);
}
/**
* Get redis.
*
* @return RedisClient
*/
public function getRedis(): RedisClient
{
return $this->redis;
}
/**
* {@inheritDoc}
*/
public function get(string $key, bool $withExpired = false)
{
$prefix = $this->getPrefix();
if (false === strpos($key, $prefix)) {
$key = $prefix . $key;
}
$contents = $this->redis->get($key);
if (null !== $contents) {
$contents = json_decode($contents, true);
}
if ($withExpired) {
return $contents;
}
if ( ! $contents) {
return null;
}
$isExpired = Carbon::parse($contents['expires_at'])->lt(Carbon::now());
return $isExpired ? null : $contents;
}
/**
* {@inheritDoc}
*/
public function set(string $key, $value)
{
$contents = $this->get($key) ?? [];
if (\is_array($value)) {
$contents = $value + $contents;
} else {
$contents[] = $value;
}
$status = $this->redis->set($this->getPrefix() . $key, json_encode($contents));
return 'OK' === $status->getPayload();
}
/**
* {@inheritDoc}
*/
public function delete(string $key): bool
{
$prefix = $this->getPrefix();
if (false === strpos($key, $prefix)) {
$key = $prefix . $key;
}
return $this->redis->del([$key]) > 0;
}
/**
* {@inheritDoc}
*/
public function keys(): array
{
return $this->redis->keys($this->getPrefix() . '*');
}
}