Skip to content

Commit 83c68d4

Browse files
committed
refactor: replase universal getOption() to simple getPrefix()
1 parent 80c7d05 commit 83c68d4

4 files changed

Lines changed: 12 additions & 29 deletions

File tree

src/Prometheus/Storage/AbstractRedis.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public function wipeStorage(): void
5757

5858
$searchPattern = '';
5959

60-
$globalPrefix = $this->redis->getOption(RedisClient::OPT_PREFIX);
61-
if (is_string($globalPrefix)) {
60+
$globalPrefix = $this->redis->getPrefix();
61+
if ($globalPrefix !== null) {
6262
$searchPattern .= $globalPrefix;
6363
}
6464

@@ -371,11 +371,13 @@ protected function collectHistograms(): array
371371

372372
protected function removePrefixFromKey(string $key): string
373373
{
374-
if ($this->redis->getOption(RedisClient::OPT_PREFIX) === null) {
374+
$prefix = $this->redis->getPrefix();
375+
376+
if ($prefix === null) {
375377
return $key;
376378
}
377379

378-
return substr($key, strlen($this->redis->getOption(RedisClient::OPT_PREFIX)));
380+
return substr($key, strlen($prefix));
379381
}
380382

381383
/**

src/Prometheus/Storage/RedisClients/PHPRedis.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88

99
class PHPRedis implements RedisClient
1010
{
11-
private const OPTIONS_MAP = [
12-
RedisClient::OPT_PREFIX => \Redis::OPT_PREFIX,
13-
];
14-
1511
/**
1612
* @var \Redis
1713
*/
@@ -57,13 +53,9 @@ public static function fromExistingConnection(\Redis $redis, array $options): se
5753
return $self;
5854
}
5955

60-
public function getOption(string $option): mixed
56+
public function getPrefix(): ?string
6157
{
62-
if (!isset(self::OPTIONS_MAP[$option])) {
63-
return null;
64-
}
65-
66-
return $this->redis->getOption(self::OPTIONS_MAP[$option]);
58+
return $this->redis->getOption(\Redis::OPT_PREFIX) ?: null;
6759
}
6860

6961
public function eval(string $script, array $args = [], int $num_keys = 0): void

src/Prometheus/Storage/RedisClients/Predis.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010

1111
class Predis implements RedisClient
1212
{
13-
private const OPTIONS_MAP = [
14-
RedisClient::OPT_PREFIX => 'prefix',
15-
];
16-
1713
private Client $client;
1814

1915
public function __construct(Client $client)
@@ -40,18 +36,13 @@ public static function fromExistingConnection(Client $client): self
4036
return new self($client);
4137
}
4238

43-
public function getOption(string $option): mixed
39+
public function getPrefix(): ?string
4440
{
45-
if (! isset(self::OPTIONS_MAP[$option])) {
46-
return null;
47-
}
48-
49-
$mappedOption = self::OPTIONS_MAP[$option];
50-
$value = $this->client->getOptions()->$mappedOption;
41+
$value = $this->client->getOptions()->prefix;
5142

5243
return $value instanceof \Predis\Command\Processor\KeyPrefixProcessor
5344
? $value->getPrefix()
54-
: $value;
45+
: null;
5546
}
5647

5748
public function eval(string $script, array $args = [], int $num_keys = 0): void

src/Prometheus/Storage/RedisClients/RedisClient.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
interface RedisClient
88
{
9-
public const OPT_PREFIX = 'prefix';
10-
11-
public function getOption(string $option): mixed;
9+
public function getPrefix(): ?string;
1210

1311
/**
1412
* @param mixed[] $args

0 commit comments

Comments
 (0)