Skip to content

Commit 4887100

Browse files
committed
Implement AbstractUpdater.
Signed-off-by: Arris Ray <arris.ray+github@gmail.com>
1 parent 943e317 commit 4887100

File tree

4 files changed

+55
-137
lines changed

4 files changed

+55
-137
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Prometheus\Storage\RedisTxn\Updater;
4+
5+
use Prometheus\Storage\RedisTxn\RedisScript\RedisScriptHelper;
6+
use Redis;
7+
8+
abstract class AbstractUpdater implements UpdaterInterface
9+
{
10+
/**
11+
* @var RedisScriptHelper
12+
*/
13+
private $helper;
14+
15+
/**
16+
* @var Redis
17+
*/
18+
private $redis;
19+
20+
/**
21+
* @param Redis $redis
22+
*/
23+
public function __construct(Redis $redis)
24+
{
25+
$this->helper = new RedisScriptHelper();
26+
$this->redis = $redis;
27+
}
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public function getHelper(): RedisScriptHelper
33+
{
34+
return $this->helper;
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function getRedis(): Redis
41+
{
42+
return $this->redis;
43+
}
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function update(array $data)
49+
{
50+
return $this->getRedisScript($data)->eval($this->getRedis());
51+
}
52+
}

src/Prometheus/Storage/RedisTxn/Updater/CounterUpdater.php

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
use Prometheus\Storage\RedisTxn\Metric\MetadataBuilder;
66
use Prometheus\Storage\RedisTxn\RedisScript\RedisScript;
7-
use Prometheus\Storage\RedisTxn\RedisScript\RedisScriptHelper;
8-
use Redis;
97

10-
class CounterUpdater implements UpdaterInterface
8+
class CounterUpdater extends AbstractUpdater
119
{
1210
/**
1311
* @var string
@@ -46,41 +44,6 @@ class CounterUpdater implements UpdaterInterface
4644
return didUpdate
4745
LUA;
4846

49-
/**
50-
* @var RedisScriptHelper
51-
*/
52-
private $helper;
53-
54-
/**
55-
* @var Redis
56-
*/
57-
private $redis;
58-
59-
/**
60-
* @param Redis $redis
61-
*/
62-
public function __construct(Redis $redis)
63-
{
64-
$this->helper = new RedisScriptHelper();
65-
$this->redis = $redis;
66-
}
67-
68-
/**
69-
* @inheritDoc
70-
*/
71-
public function getHelper(): RedisScriptHelper
72-
{
73-
return $this->helper;
74-
}
75-
76-
/**
77-
* @inheritDoc
78-
*/
79-
public function getRedis(): Redis
80-
{
81-
return $this->redis;
82-
}
83-
8447
/**
8548
* @inheritDoc
8649
*/
@@ -115,12 +78,4 @@ public function getRedisScript(array $data): RedisScript
11578
->withNumKeys(3)
11679
->build();
11780
}
118-
119-
/**
120-
* @inheritDoc
121-
*/
122-
public function update(array $data)
123-
{
124-
return $this->getRedisScript($data)->eval($this->redis);
125-
}
12681
}

src/Prometheus/Storage/RedisTxn/Updater/GaugeUpdater.php

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
use Prometheus\Storage\RedisTxn\Metric\MetadataBuilder;
66
use Prometheus\Storage\RedisTxn\RedisScript\RedisScript;
7-
use Prometheus\Storage\RedisTxn\RedisScript\RedisScriptHelper;
8-
use Redis;
97

10-
class GaugeUpdater implements UpdaterInterface
8+
class GaugeUpdater extends AbstractUpdater
119
{
1210
/**
1311
* @var string
@@ -52,42 +50,6 @@ class GaugeUpdater implements UpdaterInterface
5250
return didUpdate
5351
LUA;
5452

55-
/**
56-
* @var RedisScriptHelper
57-
*/
58-
private $helper;
59-
60-
/**
61-
* @var Redis
62-
*/
63-
private $redis;
64-
65-
/**
66-
* @param Redis $redis
67-
*/
68-
public function __construct(Redis $redis)
69-
{
70-
$this->helper = new RedisScriptHelper();
71-
$this->redis = $redis;
72-
}
73-
74-
/**
75-
* @inheritDoc
76-
*/
77-
public function getHelper(): RedisScriptHelper
78-
{
79-
$this->helper = $this->helper ?? new RedisScriptHelper();
80-
return $this->helper;
81-
}
82-
83-
/**
84-
* @inheritDoc
85-
*/
86-
public function getRedis(): Redis
87-
{
88-
return $this->redis;
89-
}
90-
9153
/**
9254
* @inheritDoc
9355
*/
@@ -122,12 +84,4 @@ public function getRedisScript(array $data): RedisScript
12284
->withNumKeys(3)
12385
->build();
12486
}
125-
126-
/**
127-
* @inheritDoc
128-
*/
129-
public function update(array $data)
130-
{
131-
return $this->getRedisScript($data)->eval($this->redis);
132-
}
13387
}

src/Prometheus/Storage/RedisTxn/Updater/SummaryUpdater.php

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
use Prometheus\Storage\RedisTxn\Metric\MetadataBuilder;
66
use Prometheus\Storage\RedisTxn\RedisScript\RedisScript;
7-
use Prometheus\Storage\RedisTxn\RedisScript\RedisScriptHelper;
8-
use Redis;
97

10-
class SummaryUpdater implements UpdaterInterface
8+
class SummaryUpdater extends AbstractUpdater
119
{
1210
/**
1311
* @var string
@@ -34,39 +32,6 @@ class SummaryUpdater implements UpdaterInterface
3432
redis.call('hset', metaHashKey, ttlFieldName, ttlFieldValue)
3533
LUA;
3634

37-
/**
38-
* @var RedisScriptHelper
39-
*/
40-
private $helper;
41-
42-
/**
43-
* @var Redis
44-
*/
45-
private $redis;
46-
47-
/**
48-
* @param Redis $redis
49-
*/
50-
public function __construct(Redis $redis)
51-
{
52-
$this->helper = new RedisScriptHelper();
53-
$this->redis = $redis;
54-
}
55-
56-
public function getHelper(): RedisScriptHelper
57-
{
58-
$this->helper = $this->helper ?? new RedisScriptHelper();
59-
return $this->helper;
60-
}
61-
62-
/**
63-
* @inheritDoc
64-
*/
65-
public function getRedis(): Redis
66-
{
67-
return $this->redis;
68-
}
69-
7035
/**
7136
* @inheritDoc
7237
*/
@@ -112,12 +77,4 @@ public function getRedisScript(array $data): RedisScript
11277
->withNumKeys(3)
11378
->build();
11479
}
115-
116-
/**
117-
* @inheritDoc
118-
*/
119-
public function update(array $data)
120-
{
121-
return $this->getRedisScript($data)->eval($this->redis);
122-
}
12380
}

0 commit comments

Comments
 (0)