|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Krenor\Prometheus\Storage\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Tightenco\Collect\Support\Collection; |
|
6
|
|
|
use Krenor\Prometheus\Contracts\Repository; |
|
7
|
|
|
use Krenor\Prometheus\Storage\Redis\Connection as Redis; |
|
8
|
|
|
|
|
9
|
|
|
class RedisRepository implements Repository |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* RedisRepository constructor. |
|
13
|
|
|
* |
|
14
|
|
|
* @param Redis $redis |
|
15
|
|
|
*/ |
|
16
|
|
|
public function __construct(protected Redis $redis) |
|
17
|
|
|
{ |
|
18
|
|
|
// |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
12 |
|
public function get(string $key): Collection |
|
25
|
|
|
{ |
|
26
|
12 |
|
return new Collection( |
|
27
|
12 |
|
!str_contains($key, ':VALUES') |
|
28
|
12 |
|
? $this->redis->hgetall($key) |
|
29
|
12 |
|
: $this->redis->lrange($key, 0, -1) |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
8 |
|
public function increment(string $key, string $field, float $value): void |
|
37
|
|
|
{ |
|
38
|
8 |
|
$this->redis->hincrbyfloat($key, $field, $value); |
|
39
|
8 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function decrement(string $key, string $field, float $value): void |
|
45
|
|
|
{ |
|
46
|
2 |
|
$this->increment($key, $field, -abs($value)); |
|
47
|
2 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function set(string $key, string $field, mixed $value): void |
|
53
|
|
|
{ |
|
54
|
4 |
|
$this->redis->hset($key, $field, $value); |
|
55
|
4 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function push(string $key, float $value): void |
|
61
|
|
|
{ |
|
62
|
2 |
|
$this->redis->rpush($key, [$value]); |
|
63
|
2 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
12 |
|
public function flush(): bool |
|
69
|
|
|
{ |
|
70
|
12 |
|
return $this->redis->flushdb(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|