|
3 | 3 | namespace UnityWebPortal\lib; |
4 | 4 |
|
5 | 5 | use Redis; |
6 | | -use Exception; |
| 6 | +use TypeError; |
7 | 7 |
|
8 | 8 | class UnityRedis |
9 | 9 | { |
@@ -70,43 +70,31 @@ public function appendCacheArray( |
70 | 70 | if (!$this->enabled) { |
71 | 71 | return; |
72 | 72 | } |
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!"); |
85 | 76 | } |
| 77 | + $new_val = $old_val + [$value]; |
| 78 | + sort($new_val); |
| 79 | + $this->setCache($object, $key, $new_val); |
86 | 80 | } |
87 | 81 |
|
88 | | - // TODO return void |
89 | 82 | public function removeCacheArray( |
90 | 83 | string $object, |
91 | 84 | string $key, |
92 | 85 | mixed $value, |
93 | 86 | callable $default_value_getter, |
94 | 87 | ) { |
95 | 88 | if (!$this->enabled) { |
96 | | - return null; |
| 89 | + return; |
97 | 90 | } |
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!"); |
109 | 94 | } |
| 95 | + $new_val = array_diff($old_val, [$value]); |
| 96 | + sort($new_val); |
| 97 | + $this->setCache($object, $key, $new_val); |
110 | 98 | } |
111 | 99 |
|
112 | 100 | public function flushAll(): void |
|
0 commit comments