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