-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathPredis.php
More file actions
111 lines (88 loc) · 2.43 KB
/
Predis.php
File metadata and controls
111 lines (88 loc) · 2.43 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
namespace Prometheus\Storage;
use Predis\Configuration\Option\Prefix;
use Prometheus\Exception\StorageException;
use Predis\Client;
/**
* @property Client $redis
*/
final class Predis extends Redis
{
/**
* @var mixed[]
*/
private static array $defaultOptions = [
'host' => '127.0.0.1',
'port' => 6379,
'scheme' => 'tcp',
'timeout' => 0.1,
'read_timeout' => '10',
'persistent' => 0,
'password' => null,
];
public function __construct(array $options = [])
{
$this->options = array_merge(self::$defaultOptions, $options);
parent::__construct($options);
$this->redis = new Client($this->options);
}
public static function fromClient(Client $redis): self
{
if ($redis->isConnected() === false) {
throw new StorageException('Connection to Redis server not established');
}
$self = new self();
$self->redis = $redis;
return $self;
}
protected function ensureOpenConnection(): void
{
if ($this->redis->isConnected() === false) {
$this->redis->connect();
}
}
public static function fromExistingConnection(\Redis $redis): Redis
{
throw new \RuntimeException('This method is not supported by predis adapter');
}
protected function getGlobalPrefix(): ?string
{
if ($this->redis->getOptions()->prefix === null) {
return null;
}
if ($this->redis->getOptions()->prefix instanceof Prefix) {
return $this->redis->getOptions()->prefix->getPrefix();
}
return null;
}
/**
* @param mixed[] $args
* @param int $keysCount
* @return mixed[]
*/
protected function evalParams(array $args, int $keysCount): array
{
return [$keysCount, ...$args];
}
protected function prefix(string $key): string
{
// the predis is doing key prefixing on its own
return '';
}
protected function setParams(array $input): array
{
$values = array_values($input);
$params = [];
if (isset($input['EX'])) {
$params[] = 'EX';
$params[] = $input['EX'];
}
if (isset($input['PX'])) {
$params[] = 'PX';
$params[] = $input['PX'];
}
$params[] = $values[0];
return $params;
}
}