|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of the phpCacheAdmin. |
| 4 | + * Copyright (c) Róbert Kelčák (https://kelcak.com/) |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace RobiNN\Pca\Dashboards\Redis\Compatibility\Cluster; |
| 10 | + |
| 11 | +use Redis; |
| 12 | +use RedisClusterException; |
| 13 | +use RobiNN\Pca\Dashboards\DashboardException; |
| 14 | +use RobiNN\Pca\Dashboards\Redis\Compatibility\RedisCompatibilityInterface; |
| 15 | +use RobiNN\Pca\Dashboards\Redis\Compatibility\RedisJson; |
| 16 | +use RobiNN\Pca\Dashboards\Redis\Compatibility\RedisModules; |
| 17 | + |
| 18 | +class RedisCluster extends \RedisCluster implements RedisCompatibilityInterface { |
| 19 | + use RedisJson; |
| 20 | + use RedisModules; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var array<int|string, string> |
| 24 | + */ |
| 25 | + public array $data_types = [ |
| 26 | + Redis::REDIS_NOT_FOUND => 'other', |
| 27 | + Redis::REDIS_STRING => 'string', |
| 28 | + Redis::REDIS_SET => 'set', |
| 29 | + Redis::REDIS_LIST => 'list', |
| 30 | + Redis::REDIS_ZSET => 'zset', |
| 31 | + Redis::REDIS_HASH => 'hash', |
| 32 | + Redis::REDIS_STREAM => 'stream', |
| 33 | + 'ReJSON-RL' => 'rejson', |
| 34 | + ]; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param array<string, mixed> $server |
| 38 | + * |
| 39 | + * @throws DashboardException |
| 40 | + */ |
| 41 | + public function __construct(array $server) { |
| 42 | + $auth = null; |
| 43 | + |
| 44 | + if (isset($server['password'])) { |
| 45 | + $auth = isset($server['username']) ? [$server['username'], $server['password']] : $server['password']; |
| 46 | + } |
| 47 | + |
| 48 | + try { |
| 49 | + parent::__construct($server['name'] ?? 'default', $server['nodes'], 3, 0, false, $auth); |
| 50 | + } catch (RedisClusterException $e) { |
| 51 | + throw new DashboardException($e->getMessage().' ['.implode(',', $server['nodes']).']'); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public function getType(string|int $type): string { |
| 56 | + return $this->data_types[$type] ?? 'unknown'; |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + public function getKeyType(string $key): string { |
| 61 | + $type = $this->type($key); |
| 62 | + |
| 63 | + if ($type === Redis::REDIS_NOT_FOUND) { |
| 64 | + $this->setOption(Redis::OPT_REPLY_LITERAL, true); |
| 65 | + $type = $this->rawCommand($key, 'TYPE', $key); |
| 66 | + } |
| 67 | + |
| 68 | + return $this->getType($type); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return array<int|string, mixed> |
| 73 | + */ |
| 74 | + public function getInfo(?string $option = null): array { |
| 75 | + static $info = []; |
| 76 | + |
| 77 | + $options = ['SERVER', 'CLIENTS', 'MEMORY', 'PERSISTENCE', 'STATS', 'REPLICATION', 'CPU', 'CLUSTER', 'KEYSPACE']; |
| 78 | + $nodes = $this->_masters(); |
| 79 | + |
| 80 | + foreach ($options as $option_name) { |
| 81 | + $combined = []; |
| 82 | + |
| 83 | + foreach ($nodes as $node) { |
| 84 | + $node_info = $this->info($node, $option_name); |
| 85 | + |
| 86 | + foreach ($node_info as $key => $value) { |
| 87 | + $combined[$key][] = $value; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + foreach ($combined as $key => $values) { |
| 92 | + $unique = array_unique($values); |
| 93 | + $combined[$key] = count($unique) === 1 ? $unique[0] : $unique; |
| 94 | + } |
| 95 | + |
| 96 | + $info[strtolower($option_name)] = $combined; |
| 97 | + } |
| 98 | + |
| 99 | + return $option !== null ? ($info[$option] ?? []) : $info; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @return array<int, string> |
| 104 | + */ |
| 105 | + public function scanKeys(string $pattern, int $count): array { |
| 106 | + $keys = []; |
| 107 | + $nodes = $this->_masters(); |
| 108 | + |
| 109 | + foreach ($nodes as $node) { |
| 110 | + $iterator = null; |
| 111 | + |
| 112 | + while (false !== ($scan = $this->scan($iterator, $node, $pattern, $count))) { |
| 113 | + foreach ($scan as $key) { |
| 114 | + $keys[] = $key; |
| 115 | + |
| 116 | + if (count($keys) >= $count) { |
| 117 | + return $keys; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return $keys; |
| 124 | + } |
| 125 | + |
| 126 | + public function listRem(string $key, string $value, int $count): int { |
| 127 | + return $this->lRem($key, $value, $count); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param array<string, string> $messages |
| 132 | + */ |
| 133 | + public function streamAdd(string $key, string $id, array $messages): string { |
| 134 | + return $this->xadd($key, $id, $messages); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @param array<int, string> $keys |
| 139 | + * |
| 140 | + * @return array<string, mixed> |
| 141 | + * |
| 142 | + * @throws RedisClusterException |
| 143 | + */ |
| 144 | + public function pipelineKeys(array $keys): array { |
| 145 | + $data = []; |
| 146 | + |
| 147 | + foreach ($keys as $key) { |
| 148 | + $ttl = $this->ttl($key); |
| 149 | + $type = $this->type($key); |
| 150 | + $size = $this->rawCommand($key, 'MEMORY', 'USAGE', $key); |
| 151 | + $scard = $this->rawCommand($key, 'SCARD', $key); |
| 152 | + $llen = $this->rawCommand($key, 'LLEN', $key); |
| 153 | + $zcard = $this->rawCommand($key, 'ZCARD', $key); |
| 154 | + $hlen = $this->rawCommand($key, 'HLEN', $key); |
| 155 | + $xlen = $this->rawCommand($key, 'XLEN', $key); |
| 156 | + |
| 157 | + $results = [$ttl, $type, $size, $scard, $llen, $zcard, $hlen, $xlen]; |
| 158 | + |
| 159 | + $type = $this->getType($results[1]); |
| 160 | + |
| 161 | + $count = match ($type) { |
| 162 | + 'set' => $results[3] ?? null, |
| 163 | + 'list' => $results[4] ?? null, |
| 164 | + 'zset' => $results[5] ?? null, |
| 165 | + 'hash' => $results[6] ?? null, |
| 166 | + 'stream' => $results[7] ?? null, |
| 167 | + default => null, |
| 168 | + }; |
| 169 | + |
| 170 | + $data[$key] = [ |
| 171 | + 'ttl' => $results[0], |
| 172 | + 'type' => $type, |
| 173 | + 'size' => $results[2] ?? 0, |
| 174 | + 'count' => is_numeric($count) ? (int) $count : null, |
| 175 | + ]; |
| 176 | + } |
| 177 | + |
| 178 | + return $data; |
| 179 | + } |
| 180 | + |
| 181 | + public function size(string $key): int { |
| 182 | + $size = $this->rawCommand($key, 'MEMORY', 'USAGE', $key); |
| 183 | + |
| 184 | + return is_int($size) ? $size : 0; |
| 185 | + } |
| 186 | + |
| 187 | + public function flushAllClusterDBs(): bool { |
| 188 | + $nodes = $this->_masters(); |
| 189 | + |
| 190 | + foreach ($nodes as $node) { |
| 191 | + $this->flushDB($node); |
| 192 | + } |
| 193 | + |
| 194 | + return true; |
| 195 | + } |
| 196 | + |
| 197 | + public function clusterDbSize(): int { |
| 198 | + $nodes = $this->_masters(); |
| 199 | + $total = 0; |
| 200 | + |
| 201 | + foreach ($nodes as $node) { |
| 202 | + $total += $this->dbSize($node); |
| 203 | + } |
| 204 | + |
| 205 | + return $total; |
| 206 | + } |
| 207 | +} |
0 commit comments