|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of phpCacheAdmin. |
| 4 | + * |
| 5 | + * Copyright (c) Róbert Kelčák (https://kelcak.com/) |
| 6 | + * |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + */ |
| 10 | + |
| 11 | +declare(strict_types=1); |
| 12 | + |
| 13 | +namespace RobiNN\Pca\Dashboards\Memcached\Compatibility; |
| 14 | + |
| 15 | +use RobiNN\Pca\Dashboards\Memcached\MemcachedException; |
| 16 | +use RobiNN\Pca\Helpers; |
| 17 | + |
| 18 | +class PHPMem implements CompatibilityInterface { |
| 19 | + use CommandTrait; |
| 20 | + |
| 21 | + /** |
| 22 | + * @const string PHPMem version. |
| 23 | + */ |
| 24 | + public const VERSION = '1.0.0'; |
| 25 | + |
| 26 | + private string $host; |
| 27 | + |
| 28 | + private int $port; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var array<string, int|string> |
| 32 | + */ |
| 33 | + protected array $server; |
| 34 | + |
| 35 | + /** |
| 36 | + * @param array<string, int|string> $server |
| 37 | + */ |
| 38 | + public function __construct(array $server = []) { |
| 39 | + $this->server = $server; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Add a server to the server pool. |
| 44 | + * |
| 45 | + * @param string $host |
| 46 | + * @param int $port |
| 47 | + * |
| 48 | + * @return bool |
| 49 | + */ |
| 50 | + public function addServer(string $host, int $port = 11211): bool { |
| 51 | + $this->host = $host; |
| 52 | + $this->port = $port; |
| 53 | + |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Semd data to the server. |
| 59 | + * |
| 60 | + * @param string $command |
| 61 | + * |
| 62 | + * @return ?string |
| 63 | + */ |
| 64 | + private function send(string $command): ?string { |
| 65 | + if (isset($this->server['path'])) { |
| 66 | + $fp = @stream_socket_client('unix://'.$this->host); |
| 67 | + } else { |
| 68 | + $fp = @fsockopen($this->host, $this->port, $error_code, $error_message, 3); |
| 69 | + } |
| 70 | + |
| 71 | + if ($fp === false) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + fwrite($fp, $command."\r\n"); |
| 76 | + |
| 77 | + $buffer = ''; |
| 78 | + |
| 79 | + while (!feof($fp)) { |
| 80 | + $buffer .= fgets($fp, 256); |
| 81 | + |
| 82 | + foreach (['END', 'DELETED', 'NOT_FOUND', 'OK', 'EXISTS', 'ERROR', 'RESET', 'STORED', 'NOT_STORED', 'VERSION'] as $end) { |
| 83 | + if (preg_match('/^'.$end.'/imu', $buffer)) { |
| 84 | + break 2; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + fclose($fp); |
| 90 | + |
| 91 | + return rtrim($buffer, "\r\n"); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Store an item. |
| 96 | + * |
| 97 | + * @param string $key |
| 98 | + * @param mixed $value |
| 99 | + * @param int $expiration |
| 100 | + * |
| 101 | + * @return bool |
| 102 | + */ |
| 103 | + public function set(string $key, $value, int $expiration = 0): bool { |
| 104 | + $type = gettype($value); |
| 105 | + |
| 106 | + if (($type !== 'string' && $type !== 'integer' && $type !== 'double' && $type !== 'boolean') === true) { |
| 107 | + $value = serialize($value); |
| 108 | + } |
| 109 | + |
| 110 | + $raw = $this->send('set'.' '.$key.' '.'0 '.$expiration.' '.strlen($value)."\r\n".$value."\r\n"); |
| 111 | + |
| 112 | + if (Helpers::str_starts_with($raw, 'STORED')) { |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Retrieve an item. |
| 121 | + * |
| 122 | + * @param string $key |
| 123 | + * |
| 124 | + * @return string|false |
| 125 | + */ |
| 126 | + public function get(string $key) { |
| 127 | + $raw = $this->send('get '.$key); |
| 128 | + $lines = explode("\r\n", $raw); |
| 129 | + |
| 130 | + if (Helpers::str_starts_with($raw, 'VALUE') && Helpers::str_ends_with($raw, 'END')) { |
| 131 | + return $lines[1]; |
| 132 | + } |
| 133 | + |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Delete item from the server. |
| 139 | + * |
| 140 | + * @param string $key |
| 141 | + * |
| 142 | + * @return bool |
| 143 | + */ |
| 144 | + public function delete(string $key): bool { |
| 145 | + $raw = $this->send('delete '.$key); |
| 146 | + |
| 147 | + return $raw === 'DELETED'; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Invalidate all items in the cache. |
| 152 | + * |
| 153 | + * @return bool |
| 154 | + */ |
| 155 | + public function flush(): bool { |
| 156 | + $raw = $this->send('flush_all'); |
| 157 | + |
| 158 | + return $raw === 'OK'; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Check connection. |
| 163 | + * |
| 164 | + * @return bool |
| 165 | + */ |
| 166 | + public function isConnected(): bool { |
| 167 | + $stats = $this->getServerStats(); |
| 168 | + |
| 169 | + return isset($stats['pid']) && $stats['pid'] > 0; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Get server statistics. |
| 174 | + * |
| 175 | + * @return array<string, mixed> |
| 176 | + */ |
| 177 | + public function getServerStats(): array { |
| 178 | + $raw = $this->send('stats'); |
| 179 | + $lines = explode("\r\n", $raw); |
| 180 | + $line_n = 0; |
| 181 | + $stats = []; |
| 182 | + |
| 183 | + while ($lines[$line_n] !== 'END') { |
| 184 | + $line = explode(' ', $lines[$line_n]); |
| 185 | + array_shift($line); // remove 'STAT' key |
| 186 | + [$name, $value] = $line; |
| 187 | + |
| 188 | + $stats[$name] = $value; |
| 189 | + |
| 190 | + ++$line_n; |
| 191 | + } |
| 192 | + |
| 193 | + return $stats; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Store item. |
| 198 | + * |
| 199 | + * @param string $key |
| 200 | + * @param string $value |
| 201 | + * @param int $expiration |
| 202 | + * |
| 203 | + * @return bool |
| 204 | + */ |
| 205 | + public function store(string $key, string $value, int $expiration = 0): bool { |
| 206 | + return $this->set($key, $value, $expiration); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * SASL authentication. |
| 211 | + * |
| 212 | + * @return void |
| 213 | + * @throws MemcachedException |
| 214 | + */ |
| 215 | + public function sasl(): void { |
| 216 | + throw new MemcachedException('PHPMem does not support SASL authentication.'); |
| 217 | + } |
| 218 | +} |
0 commit comments