|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BlueCache; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
6
|
|
|
|
|
7
|
|
|
class SimpleCache implements CacheInterface |
|
8
|
|
|
{ |
|
9
|
|
|
use Common; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @param string $key |
|
13
|
|
|
* @param mixed $default |
|
14
|
|
|
* @return array|null|\Psr\Cache\CacheItemInterface |
|
15
|
|
|
*/ |
|
16
|
2 |
|
public function get($key, $default = null) |
|
17
|
|
|
{ |
|
18
|
2 |
|
$cacheItem = $this->storage->restore($key); |
|
19
|
|
|
|
|
20
|
2 |
|
if (\is_null($cacheItem)) { |
|
21
|
|
|
return $default; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
2 |
|
return $cacheItem->get(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $key |
|
29
|
|
|
* @param mixed $value |
|
30
|
|
|
* @param \DateTimeInterface|\DateInterval|null|int $ttl |
|
31
|
|
|
* @return bool |
|
32
|
|
|
* @throws \BlueCache\CacheException |
|
33
|
|
|
*/ |
|
34
|
8 |
|
public function set($key, $value, $ttl = null) |
|
35
|
|
|
{ |
|
36
|
8 |
|
$item = (new CacheItem($key))->set($value); |
|
37
|
|
|
|
|
38
|
8 |
|
if (!\is_null($ttl)) { |
|
39
|
1 |
|
$item->expiresAfter($ttl); |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
8 |
|
return $this->storage->store($item); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $key |
|
47
|
|
|
* @return bool |
|
48
|
|
|
* @throws \BlueCache\CacheException |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function delete($key) |
|
51
|
|
|
{ |
|
52
|
1 |
|
return $this->storage->clear($key); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return bool |
|
57
|
|
|
* @throws \BlueCache\CacheException |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function clear() |
|
60
|
|
|
{ |
|
61
|
1 |
|
return $this->storage->clear(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param iterable $keys |
|
66
|
|
|
* @param null|mixed $default |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
2 |
|
public function getMultiple($keys, $default = null) |
|
70
|
|
|
{ |
|
71
|
2 |
|
$list = []; |
|
72
|
|
|
|
|
73
|
2 |
|
foreach ($keys as $key) { |
|
74
|
2 |
|
$list[$key] = $this->get($key, $default); |
|
75
|
2 |
|
} |
|
76
|
|
|
|
|
77
|
2 |
|
return $list; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param iterable $values |
|
82
|
|
|
* @param null|int $ttl |
|
83
|
|
|
* @return bool |
|
84
|
|
|
* @throws \BlueCache\CacheException |
|
85
|
|
|
*/ |
|
86
|
5 |
|
public function setMultiple($values, $ttl = null) |
|
87
|
|
|
{ |
|
88
|
5 |
|
$flag = true; |
|
89
|
|
|
|
|
90
|
5 |
|
foreach ($values as $key => $data) { |
|
91
|
5 |
|
$isSet = $this->set($key, $data, $ttl); |
|
92
|
|
|
|
|
93
|
5 |
|
if (!$isSet) { |
|
94
|
|
|
$flag = false; |
|
95
|
|
|
} |
|
96
|
5 |
|
} |
|
97
|
|
|
|
|
98
|
5 |
|
return $flag; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param iterable $keys |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function deleteMultiple($keys) |
|
106
|
|
|
{ |
|
107
|
1 |
|
return $this->storage->clearMany($keys); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $key |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
8 |
|
public function has($key) |
|
115
|
|
|
{ |
|
116
|
8 |
|
return $this->storage->exists($key); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|