1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RedisProxy; |
4
|
|
|
|
5
|
|
|
use Predis\Client; |
6
|
|
|
use Redis; |
7
|
|
|
|
8
|
|
|
class InfoHelper |
9
|
|
|
{ |
10
|
|
|
private static $keyStartToSectionMap = [ |
11
|
|
|
'redis_' => 'server', |
12
|
|
|
'uptime_' => 'server', |
13
|
|
|
'client_' => 'clients', |
14
|
|
|
'used_memory' => 'memory', |
15
|
|
|
'mem_' => 'memory', |
16
|
|
|
'rdb_' => 'persistence', |
17
|
|
|
'aof_' => 'persistence', |
18
|
|
|
'total_' => 'stats', |
19
|
|
|
'sync_' => 'stats', |
20
|
|
|
'keyspace_' => 'stats', |
21
|
|
|
'pubsub_' => 'stats', |
22
|
|
|
'repl_backlog_' => 'replication', |
23
|
|
|
'used_cpu_' => 'cpu', |
24
|
|
|
'db' => 'keyspace', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
private static $keyToSectionMap = [ |
28
|
|
|
'os' => 'server', |
29
|
|
|
'arch_bits' => 'server', |
30
|
|
|
'multiplexing_api' => 'server', |
31
|
|
|
'gcc_version' => 'server', |
32
|
|
|
'process_id' => 'server', |
33
|
|
|
'run_id' => 'server', |
34
|
|
|
'tcp_port' => 'server', |
35
|
|
|
'hz' => 'server', |
36
|
|
|
'lru_clock' => 'server', |
37
|
|
|
'config_file' => 'server', |
38
|
|
|
'connected_clients' => 'clients', |
39
|
|
|
'blocked_clients' => 'clients', |
40
|
|
|
'loading' => 'persistence', |
41
|
|
|
'instantaneous_ops_per_sec' => 'stats', |
42
|
|
|
'rejected_connections' => 'stats', |
43
|
|
|
'expired_keys' => 'stats', |
44
|
|
|
'evicted_keys' => 'stats', |
45
|
|
|
'latest_fork_usec' => 'stats', |
46
|
|
|
'role' => 'replication', |
47
|
|
|
'connected_slaves' => 'replication', |
48
|
|
|
'master_repl_offset' => 'replication', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Client|Redis $driver |
53
|
|
|
* @param array $result |
54
|
|
|
* @param integer|null $databases |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
8 |
|
public static function createInfoArray($driver, array $result, $databases = null) |
58
|
|
|
{ |
59
|
8 |
|
$groupedResult = []; |
60
|
8 |
|
self::initializeKeyspace($groupedResult, $databases); |
61
|
|
|
|
62
|
8 |
|
if ($driver instanceof Client) { |
63
|
4 |
|
return self::createInfoForPredis($result, $groupedResult); |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
return self::createInfoForRedis($result, $groupedResult); |
67
|
|
|
} |
68
|
|
|
|
69
|
8 |
|
private static function initializeKeyspace(&$groupedResult, $databases = null) |
70
|
|
|
{ |
71
|
8 |
|
if ($databases === null) { |
72
|
8 |
|
return; |
73
|
|
|
} |
74
|
4 |
|
$groupedResult['keyspace'] = []; |
75
|
4 |
|
for ($db = 0; $db < $databases; ++$db) { |
76
|
4 |
|
$groupedResult['keyspace']["db$db"] = [ |
77
|
2 |
|
'keys' => 0, |
78
|
2 |
|
'expires' => null, |
79
|
2 |
|
'avg_ttl' => null, |
80
|
|
|
]; |
81
|
2 |
|
} |
82
|
4 |
|
} |
83
|
|
|
|
84
|
4 |
|
private static function createInfoForPredis(array $result, array $groupedResult) |
85
|
|
|
{ |
86
|
4 |
|
$result = array_change_key_case($result, CASE_LOWER); |
87
|
4 |
|
if (isset($groupedResult['keyspace']) && isset($result['keyspace'])) { |
88
|
2 |
|
$groupedResult['keyspace'] = array_merge($groupedResult['keyspace'], $result['keyspace']); |
89
|
2 |
|
unset($result['keyspace']); |
90
|
1 |
|
} |
91
|
4 |
|
return array_merge($groupedResult, $result); |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
private static function createInfoForRedis(array $result, array $groupedResult) |
95
|
|
|
{ |
96
|
4 |
|
foreach ($result as $key => $value) { |
97
|
2 |
|
if (isset(self::$keyToSectionMap[$key])) { |
98
|
2 |
|
$groupedResult[self::$keyToSectionMap[$key]][$key] = $value; |
99
|
2 |
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
foreach (self::$keyStartToSectionMap as $keyStart => $targetSection) { |
103
|
2 |
|
if (strpos($key, $keyStart) === 0 && $keyStart === 'db') { |
104
|
2 |
|
$dbKeyspace = explode(',', $value); |
105
|
2 |
|
$info['keys'] = explode('=', $dbKeyspace[0])[1]; |
|
|
|
|
106
|
2 |
|
$info['expires'] = explode('=', $dbKeyspace[1])[1]; |
|
|
|
|
107
|
2 |
|
$info['avg_ttl'] = explode('=', $dbKeyspace[2])[1]; |
108
|
2 |
|
$value = $info; |
109
|
1 |
|
} |
110
|
2 |
|
if (strpos($key, $keyStart) === 0) { |
111
|
2 |
|
$groupedResult[$targetSection][$key] = $value; |
112
|
2 |
|
continue; |
113
|
|
|
} |
114
|
1 |
|
} |
115
|
2 |
|
} |
116
|
4 |
|
return $groupedResult; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.