1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RedisProxy; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @method int zcard(string $key) Get the number of members in a sorted set |
7
|
|
|
*/ |
8
|
|
|
trait SortedSetBehavior |
9
|
|
|
{ |
10
|
|
|
private $driver; |
11
|
|
|
|
12
|
|
|
abstract protected function init(); |
13
|
20 |
|
|
14
|
|
|
abstract public function actualDriver(); |
15
|
20 |
|
|
16
|
20 |
|
/** |
17
|
12 |
|
* Add one or more members to a sorted set, or update its score if it already exists |
18
|
12 |
|
* @param string $key |
19
|
12 |
|
* @param array $dictionary (score1, member1[, score2, member2]) or associative array: [member1 => score1, member2 => score2] |
20
|
12 |
|
* @return int |
21
|
|
|
*/ |
22
|
12 |
|
public function zadd($key, ...$dictionary) |
23
|
|
|
{ |
24
|
20 |
|
$this->init(); |
25
|
|
|
if (is_array($dictionary[0])) { |
26
|
|
|
$return = 0; |
27
|
|
|
foreach ($dictionary[0] as $member => $score) { |
28
|
|
|
$res = $this->zadd($key, $score, $member); |
29
|
|
|
$return += $res; |
30
|
|
|
} |
31
|
|
|
return $return; |
32
|
|
|
} |
33
|
|
|
return $this->driver->zadd($key, ...$dictionary); |
34
|
|
|
} |
35
|
4 |
|
|
36
|
|
|
/** |
37
|
4 |
|
* Return a range of members in a sorted set, by index |
38
|
4 |
|
* @param string $key |
39
|
2 |
|
* @param int $start |
40
|
|
|
* @param int $stop |
41
|
2 |
|
* @param boolean $withscores |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function zrange($key, $start, $stop, $withscores = false) |
45
|
|
|
{ |
46
|
|
|
$this->init(); |
47
|
|
|
if ($this->actualDriver() === self::DRIVER_PREDIS) { |
48
|
|
|
return $this->driver->zrange($key, $start, $stop, ['WITHSCORES' => $withscores]); |
49
|
|
|
} |
50
|
|
|
return $this->driver->zrange($key, $start, $stop, $withscores); |
51
|
|
|
} |
52
|
4 |
|
|
53
|
|
|
/** |
54
|
4 |
|
* Return a range of members in a sorted set, by index, with scores ordered from high to low |
55
|
4 |
|
* @param string $key |
56
|
2 |
|
* @param int $start |
57
|
|
|
* @param int $stop |
58
|
2 |
|
* @param boolean $withscores |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function zrevrange($key, $start, $stop, $withscores = false) |
62
|
|
|
{ |
63
|
|
|
$this->init(); |
64
|
|
|
if ($this->actualDriver() === self::DRIVER_PREDIS) { |
65
|
|
|
return $this->driver->zrevrange($key, $start, $stop, ['WITHSCORES' => $withscores]); |
66
|
|
|
} |
67
|
|
|
return $this->driver->zrevrange($key, $start, $stop, $withscores); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|