Skip to content

Commit fc9138f

Browse files
committed
Delete all items if no key or namespace provided #155
1 parent a53a2f3 commit fc9138f

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

system/cache/redis.php

+28-10
Original file line numberDiff line numberDiff line change
@@ -45,39 +45,57 @@ public function __construct($options) {
4545
}
4646
}
4747

48+
private function key($namespace, $key = '') {
49+
return $this->cachePrefix . ($namespace ? ".$namespace" : '') . $key;
50+
}
51+
4852
public function get($namespace, $key) {
49-
$data = $this->redis->get($this->options['prefix'] . $key);
53+
$data = $this->redis->get($this->key($namespace, $key));
5054

5155
return json_decode($data, true);
5256
}
5357

5458
public function set($namespace, $key, $value, $expire = null) {
55-
$expire = $expire ?? $this->options['expire'];
56-
$prefix = $this->options['prefix'];
57-
$status = $this->redis->set($prefix . $key, json_encode($value));
59+
$expire = $expire ?? $this->expire;
60+
$_key = $this->key($namespace, $key);
61+
$status = $this->redis->set($_key, json_encode($value));
5862

59-
if ($status) {
60-
$this->redis->expire($prefix . $key, $expire);
63+
if ($status && $expire) {
64+
$this->redis->expire($_key, $expire);
6165
}
6266
}
6367

6468
public function getMulti($namespace, $keys, $serverKey = false) {
6569
$result = [];
6670

6771
foreach ($keys as $key) {
68-
$result[$key] = $this->get($namespace, $key);
72+
$result[$key] = $this->get($this->key($namespace, $key));
6973
}
7074

7175
return $result;
7276
}
7377

74-
public function setMulti($namespace, $items, $expire = 0, $serverKey = false) {
78+
public function setMulti($namespace, $items, $expire = null, $serverKey = false) {
79+
$expire = $expire ?? $this->expire;
80+
7581
foreach ($items as $key => $value) {
76-
$this->set($namespace, $key, $value);
82+
$this->set($this->key($namespace, $key), $value, $expire);
7783
}
7884
}
7985

8086
public function delete($namespace, $key) {
81-
$this->redis->del($this->options['prefix'] . $key);
87+
if ($key) {
88+
$keys = $this->key($namespace, $key);
89+
} else {
90+
if ($namespace) {
91+
$keys = $this->key($namespace, '*');
92+
} else {
93+
$keys = $this->key('*');
94+
}
95+
}
96+
97+
$this->redis->del($keys);
98+
99+
return true;
82100
}
83101
}

0 commit comments

Comments
 (0)