-
Notifications
You must be signed in to change notification settings - Fork 1
/
PhpRedisStorage.php
90 lines (74 loc) · 1.93 KB
/
PhpRedisStorage.php
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
<?php
/**
* Created by solly [01.11.17 14:06]
*/
namespace insolita\cqueue\Storage;
use insolita\cqueue\Contracts\StorageInterface;
use Redis;
class PhpRedisStorage implements StorageInterface
{
/**
* @var \Redis
*/
private $redis;
public function __construct(Redis $client)
{
$this->redis = $client;
}
public function delete($key)
{
return $this->redis->del($key);
}
public function listCount($key): int
{
return $this->redis->llen($key) ?? 0;
}
public function listItems($key): array
{
return $this->redis->lrange($key, 0, -1);
}
public function listPop($key)
{
return $this->redis->rpop($key);
}
public function listPush($key, array $values)
{
if (!empty($values)) {
foreach ($values as $value) {
$this->redis->lPush($key, $value);
}
}
}
public function zSetCount($key): int
{
return $this->redis->zcard($key) ?? 0;
}
public function zSetItems($key): array
{
return $this->redis->zrange($key, 0, -1);
}
public function zSetExpiredItems($key, $score): array
{
return $this->redis->zrevrangebyscore($key, $score, '-inf');
}
public function zSetPush($key, $score, $identity)
{
return $this->redis->zadd($key, $score, $identity);
}
public function zSetRem($key, $identity)
{
return $this->redis->zrem($key, $identity);
}
public function zSetExists($key, $identity): bool
{
$score = $this->redis->zScore($key, $identity);
return ($score !== null && $score !== false);
}
public function moveFromZSetToList($listKey, $zSetKey, $item)
{
if ($this->redis->zrem($zSetKey, $item) == 1) {
$this->redis->lpush($listKey, $item);
}
//If key not in set ?
}
}