-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathRedis.php
More file actions
120 lines (100 loc) · 3.21 KB
/
Redis.php
File metadata and controls
120 lines (100 loc) · 3.21 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
112
113
114
115
116
117
118
119
120
<?php
namespace Laminas\Diagnostics\Check;
use Laminas\Diagnostics\Result\Success;
use Predis\Client as PredisClient;
use Redis as RedisExtensionClient;
use RedisException;
use RuntimeException;
use function array_key_exists;
use function class_exists;
use function is_array;
use function microtime;
/**
* Validate that a Redis service is running
*/
class Redis extends AbstractCheck
{
/** @var string|null */
protected $auth;
/** @var string */
protected $host;
/** @var int */
protected $port;
/**
* @param string $host
* @param int $port
* @param string|null $auth
*/
public function __construct($host = 'localhost', $port = 6379, $auth = null)
{
$this->host = $host;
$this->port = $port;
$this->auth = $auth;
}
/**
* Perform the check
*
* @see \Laminas\Diagnostics\Check\CheckInterface::check()
*
* @return Success
*/
public function check()
{
$client = $this->createClient();
$startTime = microtime(true);
/** @var array $stats Redis client is not in `multi` mode, so methods will directly return there response */
$stats = $client->info();
$responseTime = microtime(true) - $startTime;
$successInformation = [
"responseTime" => $responseTime,
];
if (array_key_exists('connected_clients', $stats)) {
$successInformation['connections'] = (int) $stats['connected_clients'];
} elseif (
array_key_exists('Clients', $stats) &&
is_array($stats['Clients']) &&
array_key_exists('connected_clients', $stats['Clients'])
) {
$successInformation['connections'] = (int) $stats['Clients']['connected_clients'];
}
if (array_key_exists('uptime_in_seconds', $stats)) {
$successInformation['uptime'] = (int) $stats['uptime_in_seconds'];
} elseif (
array_key_exists('Server', $stats) &&
is_array($stats['Server']) && array_key_exists('uptime_in_seconds', $stats['Server'])
) {
$successInformation['uptime'] = (int) $stats['Server']['uptime_in_seconds'];
}
return new Success(
'',
$successInformation
);
}
/**
* @return PredisClient|RedisExtensionClient
* @throws RedisException
* @throws RuntimeException
*/
private function createClient()
{
if (class_exists(RedisExtensionClient::class)) {
$client = new RedisExtensionClient();
$client->connect($this->host, $this->port);
if ($this->auth && false === $client->auth($this->auth)) {
throw new RedisException('Failed to AUTH connection');
}
return $client;
}
if (class_exists(PredisClient::class)) {
$parameters = [
'host' => $this->host,
'port' => $this->port,
];
if ($this->auth) {
$parameters['password'] = $this->auth;
}
return new PredisClient($parameters);
}
throw new RuntimeException('Neither the PHP Redis extension or Predis are installed');
}
}