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
|
3 |
|
public function get($key, $default = null) |
17
|
|
|
{ |
18
|
3 |
|
$cacheItem = $this->storage->restore($key); |
19
|
|
|
|
20
|
3 |
|
if (\is_null($cacheItem)) { |
21
|
1 |
|
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
|
9 |
|
public function set($key, $value, $ttl = null) |
35
|
|
|
{ |
36
|
9 |
|
$item = (new CacheItem($key))->set($value); |
37
|
|
|
|
38
|
9 |
|
if (!\is_null($ttl)) { |
39
|
1 |
|
$item->expiresAfter($ttl); |
40
|
1 |
|
} |
41
|
|
|
|
42
|
9 |
|
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
|
3 |
|
public function getMultiple($keys, $default = null) |
70
|
|
|
{ |
71
|
3 |
|
$list = []; |
72
|
|
|
|
73
|
3 |
|
foreach ($keys as $key) { |
74
|
3 |
|
$list[$key] = $this->get($key, $default); |
75
|
3 |
|
} |
76
|
|
|
|
77
|
3 |
|
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
|
9 |
|
public function has($key) |
115
|
|
|
{ |
116
|
9 |
|
return $this->storage->exists($key); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|