1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Properties package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Serafim\Properties; |
11
|
|
|
|
12
|
|
|
use Psr\SimpleCache\CacheInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ArrayCache |
16
|
|
|
*/ |
17
|
|
|
class ArrayCache implements CacheInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $data = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
public function get($key, $default = null) |
28
|
|
|
{ |
29
|
|
|
return $this->data[$key] ?? $default; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
|
|
public function set($key, $value, $ttl = null): bool |
36
|
|
|
{ |
37
|
|
|
$this->data[$key] = $value; |
38
|
|
|
|
39
|
|
|
return true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function delete($key): bool |
46
|
|
|
{ |
47
|
|
|
if ($this->has($key)) { |
48
|
|
|
unset($this->data[$key]); |
49
|
|
|
|
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function clear(): bool |
60
|
|
|
{ |
61
|
|
|
$this->data = []; |
62
|
|
|
|
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritdoc |
68
|
|
|
*/ |
69
|
|
|
public function getMultiple($keys, $default = null): iterable |
70
|
|
|
{ |
71
|
|
|
foreach ($keys as $key) { |
72
|
|
|
yield $this->get($key, $default); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
|
|
public function setMultiple($values, $ttl = null): bool |
80
|
|
|
{ |
81
|
|
|
foreach ($values as $key => $value) { |
82
|
|
|
$this->set($key, $value, $ttl); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @inheritdoc |
90
|
|
|
*/ |
91
|
|
|
public function deleteMultiple($keys): bool |
92
|
|
|
{ |
93
|
|
|
foreach ($keys as $key) { |
94
|
|
|
$this->delete($key); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritdoc |
102
|
|
|
*/ |
103
|
|
|
public function has($key): bool |
104
|
|
|
{ |
105
|
|
|
return isset($this->data[$key]) || \array_key_exists($key, $this->data); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|