1
|
|
|
<?php |
2
|
|
|
namespace Awssat\Visits\DataEngines; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Redis\Factory; |
6
|
|
|
|
7
|
|
|
class RedisEngine implements DataEngine |
8
|
|
|
{ |
9
|
|
|
private $redis = null; |
10
|
|
|
private $connection = null; |
11
|
|
|
private $prefix = null; |
12
|
|
|
private $isPHPRedis = true; |
13
|
|
|
|
14
|
|
|
public function __construct(Factory $redis) |
15
|
|
|
{ |
16
|
|
|
$this->redis = $redis; |
17
|
|
|
$this->isPHPRedis = strtolower(config('database.redis.client', 'phpredis')) === 'phpredis'; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function connect(string $connection): DataEngine |
21
|
|
|
{ |
22
|
|
|
$this->connection = $this->redis->connection($connection); |
23
|
|
|
return $this; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function setPrefix(string $prefix): DataEngine |
27
|
|
|
{ |
28
|
|
|
$this->prefix = $prefix . ':'; |
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function increment(string $key, int $value, $member = null): bool |
33
|
|
|
{ |
34
|
|
|
if (! empty($member) || is_numeric($member)) { |
35
|
|
|
$this->connection->zincrby($this->prefix.$key, $value, $member); |
36
|
|
|
} else { |
37
|
|
|
$this->connection->incrby($this->prefix.$key, $value); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// both methods returns integer and raise an exception in case of an error. |
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function decrement(string $key, int $value, $member = null): bool |
45
|
|
|
{ |
46
|
|
|
return $this->increment($key, -$value, $member); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function delete($key, $member = null): bool |
50
|
|
|
{ |
51
|
|
|
if(is_array($key)) { |
52
|
|
|
array_walk($key, function($item) { |
53
|
|
|
$this->delete($item); |
54
|
|
|
}); |
55
|
|
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if(! empty($member) || is_numeric($member)) { |
59
|
|
|
return $this->connection->zrem($this->prefix.$key, $member) > 0; |
60
|
|
|
} else { |
61
|
|
|
return $this->connection->del($this->prefix.$key) > 0; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function get(string $key, $member = null) |
66
|
|
|
{ |
67
|
|
|
if(! empty($member) || is_numeric($member)) { |
68
|
|
|
return $this->connection->zscore($this->prefix.$key, $member); |
69
|
|
|
} else { |
70
|
|
|
return $this->connection->get($this->prefix.$key); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function set(string $key, $value, $member = null): bool |
75
|
|
|
{ |
76
|
|
|
if(! empty($member) || is_numeric($member)) { |
77
|
|
|
return $this->connection->zAdd($this->prefix.$key, $value, $member) > 0; |
78
|
|
|
} else { |
79
|
|
|
return (bool) $this->connection->set($this->prefix.$key, $value); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function search(string $word, bool $noPrefix = true): array |
84
|
|
|
{ |
85
|
|
|
return array_map( |
86
|
|
|
function($item) use($noPrefix) { |
87
|
|
|
if ($noPrefix && substr($item, 0, strlen($this->prefix)) == $this->prefix) { |
88
|
|
|
return substr($item, strlen($this->prefix)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $item; |
92
|
|
|
}, |
93
|
|
|
$this->connection->keys($this->prefix.$word) ?? [] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function flatList(string $key, int $limit = -1): array |
98
|
|
|
{ |
99
|
|
|
return $this->connection->lrange($this->prefix.$key, 0, $limit); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function addToFlatList(string $key, $value): bool |
103
|
|
|
{ |
104
|
|
|
return $this->connection->rpush($this->prefix.$key, $value) !== false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function valueList(string $key, int $limit = -1, bool $orderByAsc = false, bool $withValues = false): array |
108
|
|
|
{ |
109
|
|
|
$range = $orderByAsc ? 'zrange' : 'zrevrange'; |
110
|
|
|
|
111
|
|
|
return $this->connection->$range($this->prefix.$key, 0, $limit, $this->isPHPRedis ? $withValues : ['withscores' => $withValues]) ?: []; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function exists(string $key): bool |
115
|
|
|
{ |
116
|
|
|
return (bool) $this->connection->exists($this->prefix.$key); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function timeLeft(string $key): int |
120
|
|
|
{ |
121
|
|
|
return $this->connection->ttl($this->prefix.$key); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function setExpiration(string $key, int $time): bool |
125
|
|
|
{ |
126
|
|
|
return $this->connection->expire($this->prefix.$key, $time); |
127
|
|
|
} |
128
|
|
|
} |