Skip to content

Commit 9c8064b

Browse files
committed
Update PHPMem.php
1 parent 8dba65c commit 9c8064b

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

src/Dashboards/Memcached/PHPMem.php

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,8 @@ public function __construct(protected array $server = []) {
5454
* @throws MemcachedException
5555
*/
5656
public function set(string $key, mixed $value, int $expiration = 0): bool {
57-
$type = gettype($value);
58-
59-
if ($type !== 'string' && $type !== 'integer' && $type !== 'double' && $type !== 'boolean') {
60-
$value = serialize($value);
61-
}
62-
63-
$raw = $this->runCommand('set '.$key.' 0 '.$expiration.' '.strlen((string) $value)."\r\n".$value);
57+
$value = is_scalar($value) ? (string) $value : serialize($value);
58+
$raw = $this->runCommand('set '.$key.' 0 '.$expiration.' '.strlen($value)."\r\n".$value);
6459

6560
return str_starts_with($raw, 'STORED');
6661
}
@@ -87,9 +82,7 @@ public function get(string $key): string|false {
8782
* @throws MemcachedException
8883
*/
8984
public function delete(string $key): bool {
90-
$raw = $this->runCommand('delete '.$key);
91-
92-
return $raw === 'DELETED';
85+
return $this->runCommand('delete '.$key) === 'DELETED';
9386
}
9487

9588
/**
@@ -98,9 +91,7 @@ public function delete(string $key): bool {
9891
* @throws MemcachedException
9992
*/
10093
public function flush(): bool {
101-
$raw = $this->runCommand('flush_all');
102-
103-
return $raw === 'OK';
94+
return $this->runCommand('flush_all') === 'OK';
10495
}
10596

10697
/**
@@ -110,18 +101,13 @@ public function flush(): bool {
110101
*/
111102
public function getServerStats(): array {
112103
$raw = $this->runCommand('stats');
113-
$lines = explode("\r\n", $raw);
114-
$line_n = 0;
115104
$stats = [];
116105

117-
while ($lines[$line_n] !== 'END') {
118-
$line = explode(' ', $lines[$line_n]);
119-
array_shift($line); // remove 'STAT'
120-
[$name, $value] = $line;
121-
122-
$stats[$name] = $value;
123-
124-
$line_n++;
106+
foreach (explode("\r\n", $raw) as $line) {
107+
if (str_starts_with($line, 'STAT')) {
108+
[, $key, $value] = explode(' ', $line, 3);
109+
$stats[$key] = $value;
110+
}
125111
}
126112

127113
return $stats;
@@ -130,11 +116,11 @@ public function getServerStats(): array {
130116
public function isConnected(): bool {
131117
try {
132118
$stats = $this->getServerStats();
119+
120+
return isset($stats['pid']) && $stats['pid'] > 0;
133121
} catch (MemcachedException) {
134122
return false;
135123
}
136-
137-
return isset($stats['pid']) && $stats['pid'] > 0;
138124
}
139125

140126
/**

0 commit comments

Comments
 (0)