Skip to content

Commit e21f0f2

Browse files
committed
rewrite
1 parent 5916ab7 commit e21f0f2

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

resources/lib/UnityRedis.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace UnityWebPortal\lib;
44

55
use Redis;
6-
use Exception;
6+
use TypeError;
77

88
class UnityRedis
99
{
@@ -70,43 +70,31 @@ public function appendCacheArray(
7070
if (!$this->enabled) {
7171
return;
7272
}
73-
74-
$cached_val = $this->getCache($object, $key);
75-
if (is_null($cached_val)) {
76-
$this->setCache($object, $key, $default_value_getter());
77-
} else {
78-
if (!is_array($cached_val)) {
79-
throw new Exception("This cache value is not an array");
80-
}
81-
82-
array_push($cached_val, $value);
83-
sort($cached_val);
84-
$this->setCache($object, $key, $cached_val);
73+
$old_val = $this->getCache($object, $key) ?? $default_value_getter();
74+
if (!is_array($old_val)) {
75+
throw new TypeError("redis[$object][$key] is not an array!");
8576
}
77+
$new_val = $old_val + [$value];
78+
sort($new_val);
79+
$this->setCache($object, $key, $new_val);
8680
}
8781

88-
// TODO return void
8982
public function removeCacheArray(
9083
string $object,
9184
string $key,
9285
mixed $value,
9386
callable $default_value_getter,
9487
) {
9588
if (!$this->enabled) {
96-
return null;
89+
return;
9790
}
98-
99-
$cached_val = $this->getCache($object, $key);
100-
if (is_null($cached_val)) {
101-
$this->setCache($object, $key, $default_value_getter());
102-
} else {
103-
if (!is_array($cached_val)) {
104-
throw new Exception("This cache value is not an array");
105-
}
106-
107-
$cached_val = array_diff($cached_val, [$value]);
108-
$this->setCache($object, $key, $cached_val);
91+
$old_val = $this->getCache($object, $key) ?? $default_value_getter();
92+
if (!is_array($old_val)) {
93+
throw new TypeError("redis[$object][$key] is not an array!");
10994
}
95+
$new_val = array_diff($old_val, [$value]);
96+
sort($new_val);
97+
$this->setCache($object, $key, $new_val);
11098
}
11199

112100
public function flushAll(): void

0 commit comments

Comments
 (0)