1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DucCnzj\Ip; |
4
|
|
|
|
5
|
|
|
use DucCnzj\Ip\Imp\CacheStoreImp; |
6
|
|
|
|
7
|
|
|
class CacheStore implements CacheStoreImp, \Countable |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The array of stored values. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $storage = []; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Retrieve an item from the cache by key. |
18
|
|
|
* |
19
|
|
|
* @param string|array $key |
20
|
|
|
* |
21
|
|
|
* @return mixed |
22
|
|
|
*/ |
23
|
10 |
|
public function get($key) |
24
|
|
|
{ |
25
|
10 |
|
return $this->storage[$key] ?? null; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Retrieve multiple items from the cache by key. |
30
|
|
|
* |
31
|
|
|
* Items not found in the cache will have a null value. |
32
|
|
|
* |
33
|
|
|
* @param array $keys |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
1 |
|
public function many(array $keys) |
38
|
|
|
{ |
39
|
1 |
|
$result = []; |
40
|
|
|
|
41
|
1 |
|
foreach ($keys as $key) { |
42
|
1 |
|
$result[] = $this->get($key); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
return $result; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Store an item in the cache for a given number of minutes. |
50
|
|
|
* |
51
|
|
|
* @param string $key |
52
|
|
|
* @param mixed $value |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
9 |
|
public function put($key, $value) |
57
|
|
|
{ |
58
|
9 |
|
$this->storage[$key] = $value; |
59
|
9 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Store multiple items in the cache for a given number of minutes. |
63
|
|
|
* |
64
|
|
|
* @param array $values |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
2 |
|
public function putMany(array $values) |
69
|
|
|
{ |
70
|
2 |
|
foreach ($values as $key => $value) { |
71
|
2 |
|
$this->put($key, $value); |
72
|
|
|
} |
73
|
2 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Remove an item from the cache. |
77
|
|
|
* |
78
|
|
|
* @param string $key |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
1 |
|
public function forget($key) |
82
|
|
|
{ |
83
|
1 |
|
unset($this->storage[$key]); |
84
|
|
|
|
85
|
1 |
|
return true; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Remove all items from the cache. |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
1 |
|
public function flush() |
94
|
|
|
{ |
95
|
1 |
|
$this->storage = []; |
96
|
|
|
|
97
|
1 |
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the cache key prefix. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
1 |
|
public function getPrefix() |
106
|
|
|
{ |
107
|
1 |
|
return ''; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return array |
112
|
|
|
* |
113
|
|
|
* @author duc <[email protected]> |
114
|
|
|
*/ |
115
|
1 |
|
public function getAllItems(): array |
116
|
|
|
{ |
117
|
1 |
|
return $this->storage; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Count elements of an object |
122
|
|
|
* |
123
|
|
|
* @link http://php.net/manual/en/countable.count.php |
124
|
|
|
* @return int The custom count as an integer. |
125
|
|
|
* </p> |
126
|
|
|
* <p> |
127
|
|
|
* The return value is cast to an integer. |
128
|
|
|
* @since 5.1.0 |
129
|
|
|
*/ |
130
|
2 |
|
public function count() |
131
|
|
|
{ |
132
|
2 |
|
return count($this->storage); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|