1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by solly [01.11.17 14:06] |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace insolita\cqueue\Storage; |
7
|
|
|
|
8
|
|
|
use insolita\cqueue\Contracts\StorageInterface; |
9
|
|
|
use Redis; |
10
|
|
|
|
11
|
|
|
class PhpRedisStorage implements StorageInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \Redis |
15
|
|
|
*/ |
16
|
|
|
private $redis; |
17
|
|
|
|
18
|
|
|
public function __construct(Redis $client) |
19
|
|
|
{ |
20
|
|
|
$this->redis = $client; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function delete($key) |
24
|
|
|
{ |
25
|
|
|
return $this->redis->del($key); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function listCount($key): int |
29
|
|
|
{ |
30
|
|
|
return $this->redis->llen($key) ?? 0; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function listItems($key): array |
34
|
|
|
{ |
35
|
|
|
return $this->redis->lrange($key, 0, -1); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function listPop($key) |
39
|
|
|
{ |
40
|
|
|
return $this->redis->rpop($key); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function listPush($key, array $values) |
44
|
|
|
{ |
45
|
|
|
if (!empty($values)) { |
46
|
|
|
foreach ($values as $value) { |
47
|
|
|
$this->redis->lPush($key, $value); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function zSetCount($key): int |
53
|
|
|
{ |
54
|
|
|
return $this->redis->zcard($key) ?? 0; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function zSetItems($key): array |
58
|
|
|
{ |
59
|
|
|
return $this->redis->zrange($key, 0, -1); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function zSetExpiredItems($key, $score): array |
63
|
|
|
{ |
64
|
|
|
return $this->redis->zrevrangebyscore($key, $score, '-inf'); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function zSetPush($key, $score, $identity) |
68
|
|
|
{ |
69
|
|
|
return $this->redis->zadd($key, $score, $identity); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function zSetRem($key, $identity) |
73
|
|
|
{ |
74
|
|
|
return $this->redis->zrem($key, $identity); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function zSetExists($key, $identity): bool |
78
|
|
|
{ |
79
|
|
|
$score = $this->redis->zScore($key, $identity); |
80
|
|
|
return ($score !== null && $score !== false); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function moveFromZSetToList($listKey, $zSetKey, $item) |
84
|
|
|
{ |
85
|
|
|
if ($this->redis->zrem($zSetKey, $item) == 1) { |
86
|
|
|
$this->redis->lpush($listKey, $item); |
87
|
|
|
} |
88
|
|
|
//If key not in set ? |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|