-
Notifications
You must be signed in to change notification settings - Fork 2
/
Settings.php
56 lines (45 loc) · 1.44 KB
/
Settings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
namespace ManaPHP;
use ManaPHP\Di\Attribute\Autowired;
use ManaPHP\Exception\InvalidArgumentException;
use ManaPHP\Redis\RedisDbInterface;
class Settings implements SettingsInterface
{
#[Autowired] protected RedisDbInterface $redisDb;
#[Autowired] protected string $key = 'settings';
#[Autowired] protected int $ttl = 1;
public function getInternal(string $key, ?string $default = null): ?string
{
if (($value = $this->redisDb->hGet($this->key, $key)) === false) {
if ($default === null) {
throw new InvalidArgumentException(['`{1}` key is not exists', $key]);
} else {
$value = $default;
}
}
return $value;
}
public function get(string $key, ?string $default = null): ?string
{
if ($this->ttl <= 0) {
return $this->getInternal($key, $default);
} else {
return apcu_remember($this->key . ':' . $key, $this->ttl, fn() => $this->getInternal($key, $default));
}
}
public function set(string $key, string $value): static
{
$this->redisDb->hSet($this->key, $key, $value);
return $this;
}
public function exists(string $key): bool
{
return $this->redisDb->hExists($this->key, $key);
}
public function delete(string $key): static
{
$this->redisDb->hDel($this->key, $key);
return $this;
}
}