1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Roave\DoctrineSimpleCache; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Cache\Cache as DoctrineCache; |
7
|
|
|
use Doctrine\Common\Cache\ClearableCache; |
8
|
|
|
use Doctrine\Common\Cache\MultiGetCache; |
9
|
|
|
use Doctrine\Common\Cache\MultiPutCache; |
10
|
|
|
use Psr\SimpleCache\CacheInterface as PsrCache; |
11
|
|
|
|
12
|
|
|
final class SimpleCacheAdapter implements PsrCache |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var DoctrineCache|ClearableCache|MultiGetCache|MultiPutCache |
16
|
|
|
*/ |
17
|
|
|
private $doctrineCache; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param DoctrineCache $doctrineCache |
21
|
|
|
* @throws \Roave\DoctrineSimpleCache\CacheException |
22
|
|
|
*/ |
23
|
72 |
|
public function __construct(DoctrineCache $doctrineCache) |
24
|
|
|
{ |
25
|
72 |
|
$this->doctrineCache = $doctrineCache; |
26
|
|
|
|
27
|
72 |
|
if (!$this->doctrineCache instanceof ClearableCache) { |
28
|
1 |
|
throw CacheException::fromNonClearableCache($this->doctrineCache); |
29
|
|
|
} |
30
|
71 |
|
if (!$this->doctrineCache instanceof MultiGetCache) { |
31
|
1 |
|
throw CacheException::fromNonMultiGetCache($this->doctrineCache); |
32
|
|
|
} |
33
|
70 |
|
if (!$this->doctrineCache instanceof MultiPutCache) { |
34
|
1 |
|
throw CacheException::fromNonMultiPutCache($this->doctrineCache); |
35
|
|
|
} |
36
|
69 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritDoc} |
40
|
|
|
*/ |
41
|
29 |
|
public function get($key, $default = null) |
42
|
|
|
{ |
43
|
29 |
|
$value = $this->doctrineCache->fetch($key); |
|
|
|
|
44
|
29 |
|
if ($value === false) { |
45
|
11 |
|
return $default; |
46
|
|
|
} |
47
|
|
|
|
48
|
22 |
|
return $value; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritDoc} |
53
|
|
|
*/ |
54
|
37 |
View Code Duplication |
public function set($key, $value, $ttl = null) : bool |
|
|
|
|
55
|
|
|
{ |
56
|
37 |
|
if ($ttl !== null) { |
57
|
13 |
|
if ($ttl instanceof \DateInterval) { |
58
|
1 |
|
$ttl = (new \DateTime())->setTimeStamp(0)->add($ttl)->getTimeStamp(); |
59
|
|
|
} |
60
|
|
|
|
61
|
13 |
|
if (!is_integer($ttl)) { |
62
|
10 |
|
throw new InvalidArgumentException; |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
if ($ttl <= 0) { |
66
|
1 |
|
return $this->doctrineCache->delete($key); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
27 |
|
return $this->doctrineCache->save($key, $value, $ttl); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
76
|
7 |
|
public function delete($key) : bool |
77
|
|
|
{ |
78
|
7 |
|
return $this->doctrineCache->delete($key); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritDoc} |
83
|
|
|
*/ |
84
|
59 |
|
public function clear() : bool |
85
|
|
|
{ |
86
|
59 |
|
return $this->doctrineCache->deleteAll(); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritDoc} |
91
|
|
|
*/ |
92
|
12 |
|
public function getMultiple($keys, $default = null) |
93
|
|
|
{ |
94
|
12 |
|
return array_merge(array_fill_keys($keys, $default), $this->doctrineCache->fetchMultiple($keys)); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritDoc} |
99
|
|
|
*/ |
100
|
24 |
View Code Duplication |
public function setMultiple($values, $ttl = null) : bool |
|
|
|
|
101
|
|
|
{ |
102
|
24 |
|
if ($ttl !== null) { |
103
|
12 |
|
if ($ttl instanceof \DateInterval) { |
104
|
1 |
|
$ttl = (new \DateTime())->setTimeStamp(0)->add($ttl)->getTimeStamp(); |
105
|
|
|
} |
106
|
|
|
|
107
|
12 |
|
if (!is_integer($ttl)) { |
108
|
10 |
|
throw new InvalidArgumentException; |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
if ($ttl <= 0) { |
112
|
1 |
|
return $this->deleteMultiple(array_keys($values)); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
13 |
|
return $this->doctrineCache->saveMultiple($values, $ttl); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritDoc} |
121
|
|
|
*/ |
122
|
5 |
|
public function deleteMultiple($keys) : bool |
123
|
|
|
{ |
124
|
5 |
|
$success = true; |
125
|
|
|
|
126
|
5 |
|
foreach ($keys as $key) { |
127
|
5 |
|
if (!$this->delete($key)) { |
128
|
5 |
|
$success = false; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
5 |
|
return $success; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritDoc} |
137
|
|
|
*/ |
138
|
3 |
|
public function has($key) : bool |
139
|
|
|
{ |
140
|
3 |
|
return $this->doctrineCache->contains($key); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: