Skip to content

Commit 003594e

Browse files
committed
Update Memcached classes
1 parent 1d28b2a commit 003594e

8 files changed

Lines changed: 81 additions & 22 deletions

File tree

src/Dashboards/Memcached/Compatibility/CommandTrait.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace RobiNN\Pca\Dashboards\Memcached\Compatibility;
1414

15-
use RobiNN\Pca\Dashboards\DashboardException;
15+
use RobiNN\Pca\Dashboards\Memcached\MemcachedException;
1616

1717
trait CommandTrait {
1818
/**
@@ -21,11 +21,9 @@ trait CommandTrait {
2121
* @param string $command https://github.com/memcached/memcached/wiki/Commands
2222
*
2323
* @return ?array<int, mixed>
24-
* @throws DashboardException
24+
* @throws MemcachedException
2525
*/
2626
public function command(string $command): ?array {
27-
$data = [];
28-
2927
if (isset($this->server['path'])) {
3028
$fp = @stream_socket_client('unix://'.$this->server['path'], $error_code, $error_message);
3129
} else {
@@ -35,7 +33,7 @@ public function command(string $command): ?array {
3533
}
3634

3735
if ($error_message !== '') {
38-
throw new DashboardException('command() method: '.$error_message);
36+
throw new MemcachedException('command() method: '.$error_message);
3937
}
4038

4139
if ($fp === false) {
@@ -45,6 +43,7 @@ public function command(string $command): ?array {
4543
fwrite($fp, $command."\n");
4644

4745
$part = '';
46+
$data = [];
4847

4948
while (true) {
5049
$part .= fgets($fp, 1024);
@@ -101,11 +100,16 @@ private function keyData(string $line): array {
101100
* https://github.com/memcached/memcached/wiki/ReleaseNotes1418#lru-crawler
102101
*
103102
* @return array<int, mixed>
104-
* @throws DashboardException
103+
* @throws MemcachedException
105104
*/
106105
public function getKeys(): array {
107106
static $keys = [];
108107

108+
if (isset($this->server['sasl_username'], $this->server['sasl_password'])) {
109+
// for future updates, currently it is not possible to list all keys with enabled SASL
110+
return [];
111+
}
112+
109113
$all_keys = $this->command('lru_crawler metadump all');
110114

111115
if ($all_keys !== null) {
@@ -123,7 +127,7 @@ public function getKeys(): array {
123127
* @param string $key
124128
*
125129
* @return string|false
126-
* @throws DashboardException
130+
* @throws MemcachedException
127131
*/
128132
public function getKey(string $key) {
129133
$data = $this->command('get '.$key);
@@ -141,7 +145,7 @@ public function getKey(string $key) {
141145
* @param string $key
142146
*
143147
* @return bool
144-
* @throws DashboardException
148+
* @throws MemcachedException
145149
*/
146150
public function exists(string $key): bool {
147151
return $this->getKey($key) !== false;

src/Dashboards/Memcached/Compatibility/CompatibilityInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,11 @@ public function store(string $key, string $value, int $expiration = 0): bool;
4444
* @return array<int, mixed>
4545
*/
4646
public function getKeys(): array;
47+
48+
/**
49+
* SASL authentication.
50+
*
51+
* @return void
52+
*/
53+
public function sasl(): void;
4754
}

src/Dashboards/Memcached/Compatibility/Memcache.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace RobiNN\Pca\Dashboards\Memcached\Compatibility;
1414

15+
use RobiNN\Pca\Dashboards\Memcached\MemcachedException;
16+
1517
class Memcache extends \Memcache implements CompatibilityInterface {
1618
use CommandTrait;
1719

@@ -59,4 +61,14 @@ public function getServerStats(): array {
5961
public function store(string $key, string $value, int $expiration = 0): bool {
6062
return $this->set($key, $value, 0, $expiration);
6163
}
64+
65+
/**
66+
* SASL authentication.
67+
*
68+
* @return void
69+
* @throws MemcachedException
70+
*/
71+
public function sasl(): void {
72+
throw new MemcachedException('Memcache does not support SASL authentication, use Memcached extension.');
73+
}
6274
}

src/Dashboards/Memcached/Compatibility/Memcached.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,14 @@ public function getServerStats(): array {
5959
public function store(string $key, string $value, int $expiration = 0): bool {
6060
return $this->set($key, $value, $expiration);
6161
}
62+
63+
/**
64+
* SASL authentication.
65+
*
66+
* @return void
67+
*/
68+
public function sasl(): void {
69+
$this->setOption(self::OPT_BINARY_PROTOCOL, true);
70+
$this->setSaslAuthData($this->server['sasl_username'], $this->server['sasl_password']);
71+
}
6272
}

src/Dashboards/Memcached/MemcachedDashboard.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ private function connect(array $server) {
8484
$memcached->addServer($server['host'], (int) $server['port']);
8585
}
8686

87+
if (isset($server['sasl_username'], $server['sasl_password'])) {
88+
try {
89+
$memcached->sasl();
90+
} catch (MemcachedException $e) {
91+
throw new DashboardException($e->getMessage());
92+
}
93+
}
94+
8795
if (!$memcached->isConnected()) {
8896
throw new DashboardException(sprintf('Failed to connect to Memcache(d) server %s.', $memcached_server));
8997
}
@@ -113,7 +121,7 @@ public function ajax(): string {
113121
if (isset($_GET['delete'])) {
114122
$return = $this->deleteKey($memcached);
115123
}
116-
} catch (DashboardException $e) {
124+
} catch (DashboardException|MemcachedException $e) {
117125
$return = $e->getMessage();
118126
}
119127
}
@@ -186,7 +194,7 @@ public function dashboard(): string {
186194
} else {
187195
$return = $this->mainDashboard($memcached);
188196
}
189-
} catch (DashboardException $e) {
197+
} catch (DashboardException|MemcachedException $e) {
190198
return $e->getMessage();
191199
}
192200
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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;
14+
15+
use Exception;
16+
17+
class MemcachedException extends Exception {
18+
}

src/Dashboards/Memcached/MemcachedTrait.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private function serverInfo(array $servers): array {
4444
'Used' => Format::bytes((int) $server_info['bytes']),
4545
'Keys' => Format::number(count($memcached->getKeys())),
4646
];
47-
} catch (DashboardException $e) {
47+
} catch (DashboardException|MemcachedException $e) {
4848
return [
4949
'error' => $e->getMessage(),
5050
];
@@ -74,7 +74,7 @@ private function deleteAllKeys($memcached): string {
7474
* @param Memcache|Memcached $memcached
7575
*
7676
* @return string
77-
* @throws DashboardException
77+
* @throws MemcachedException
7878
*/
7979
private function deleteKey($memcached): string {
8080
$keys = explode(',', Http::get('delete'));
@@ -126,7 +126,7 @@ private function moreInfo(array $servers): string {
126126
* @param Memcache|Memcached $memcached
127127
*
128128
* @return array<int, array<string, string|int>>
129-
* @throws DashboardException
129+
* @throws MemcachedException
130130
*/
131131
private function getAllKeys($memcached): array {
132132
static $keys = [];
@@ -148,7 +148,7 @@ private function getAllKeys($memcached): array {
148148
* @param Memcache|Memcached $memcached
149149
*
150150
* @return string
151-
* @throws DashboardException
151+
* @throws MemcachedException
152152
*/
153153
private function mainDashboard($memcached): string {
154154
$keys = $this->getAllKeys($memcached);
@@ -174,7 +174,7 @@ private function mainDashboard($memcached): string {
174174
* @param Memcache|Memcached $memcached
175175
*
176176
* @return string
177-
* @throws DashboardException
177+
* @throws MemcachedException
178178
*/
179179
private function viewKey($memcached): string {
180180
$key = Http::get('key');
@@ -222,7 +222,7 @@ private function viewKey($memcached): string {
222222
* @param Memcache|Memcached $memcached
223223
*
224224
* @return void
225-
* @throws DashboardException
225+
* @throws MemcachedException
226226
*/
227227
private function import($memcached): void {
228228
if ($_FILES['import']['type'] === 'text/plain') {
@@ -244,7 +244,7 @@ private function import($memcached): void {
244244
* @param Memcache|Memcached $memcached
245245
*
246246
* @return string
247-
* @throws DashboardException
247+
* @throws MemcachedException
248248
*/
249249
private function form($memcached): string {
250250
$key = Http::get('key');

tests/Dashboards/MemcachedTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
namespace Tests\Dashboards;
1414

15-
use RobiNN\Pca\Dashboards\DashboardException;
1615
use RobiNN\Pca\Dashboards\Memcached\Compatibility\Memcache;
1716
use RobiNN\Pca\Dashboards\Memcached\Compatibility\Memcached;
1817
use RobiNN\Pca\Dashboards\Memcached\MemcachedDashboard;
18+
use RobiNN\Pca\Dashboards\Memcached\MemcachedException;
1919
use RobiNN\Pca\Http;
2020
use RobiNN\Pca\Template;
2121
use Tests\TestCase;
@@ -49,7 +49,7 @@ public function testDeleteKey(): void {
4949
self::callMethod($this->dashboard, 'deleteKey', $this->memcached)
5050
);
5151
$this->assertFalse($this->memcached->exists($key));
52-
} catch (DashboardException $e) {
52+
} catch (MemcachedException $e) {
5353
echo $e->getMessage();
5454
}
5555
}
@@ -73,7 +73,7 @@ public function testDeleteKeys(): void {
7373
$this->assertFalse($this->memcached->exists($key1));
7474
$this->assertFalse($this->memcached->exists($key2));
7575
$this->assertFalse($this->memcached->exists($key3));
76-
} catch (DashboardException $e) {
76+
} catch (MemcachedException $e) {
7777
echo $e->getMessage();
7878
}
7979
}
@@ -111,7 +111,7 @@ public function testGetKey(): void {
111111
foreach ($keys as $key => $value) {
112112
$this->memcached->delete('pu-test-'.$key);
113113
}
114-
} catch (DashboardException $e) {
114+
} catch (MemcachedException $e) {
115115
echo $e->getMessage();
116116
}
117117
}
@@ -130,7 +130,7 @@ public function testSaveKey(): void {
130130
$this->assertSame('test-value', $this->memcached->getKey($key));
131131

132132
$this->memcached->delete($key);
133-
} catch (DashboardException $e) {
133+
} catch (MemcachedException $e) {
134134
echo $e->getMessage();
135135
}
136136
}

0 commit comments

Comments
 (0)