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
|
12 |
|
public function __construct(DoctrineCache $doctrineCache) |
24
|
|
|
{ |
25
|
12 |
|
$this->doctrineCache = $doctrineCache; |
26
|
|
|
|
27
|
12 |
|
if (!$this->doctrineCache instanceof ClearableCache) { |
28
|
1 |
|
throw CacheException::fromNonClearableCache($this->doctrineCache); |
29
|
|
|
} |
30
|
11 |
|
if (!$this->doctrineCache instanceof MultiGetCache) { |
31
|
1 |
|
throw CacheException::fromNonMultiGetCache($this->doctrineCache); |
32
|
|
|
} |
33
|
10 |
|
if (!$this->doctrineCache instanceof MultiPutCache) { |
34
|
1 |
|
throw CacheException::fromNonMultiPutCache($this->doctrineCache); |
35
|
|
|
} |
36
|
9 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritDoc} |
40
|
|
|
*/ |
41
|
1 |
|
public function get($key, $default = null) |
42
|
|
|
{ |
43
|
1 |
|
return $this->doctrineCache->fetch($key); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
1 |
|
public function set($key, $value, $ttl = null) : bool |
50
|
|
|
{ |
51
|
1 |
|
return $this->doctrineCache->save($key, $value, $ttl); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritDoc} |
56
|
|
|
*/ |
57
|
3 |
|
public function delete($key) : bool |
58
|
|
|
{ |
59
|
3 |
|
return $this->doctrineCache->delete($key); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
1 |
|
public function clear() : bool |
66
|
|
|
{ |
67
|
1 |
|
return $this->doctrineCache->deleteAll(); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
1 |
|
public function getMultiple($keys, $default = null) |
74
|
|
|
{ |
75
|
1 |
|
return $this->doctrineCache->fetchMultiple($keys); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
1 |
|
public function setMultiple($values, $ttl = null) : bool |
82
|
|
|
{ |
83
|
1 |
|
return $this->doctrineCache->saveMultiple($values, $ttl); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
*/ |
89
|
2 |
|
public function deleteMultiple($keys) : bool |
90
|
|
|
{ |
91
|
2 |
|
$success = true; |
92
|
|
|
|
93
|
2 |
|
foreach ($keys as $key) { |
94
|
2 |
|
if (!$this->delete($key)) { |
95
|
2 |
|
$success = false; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
return $success; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
1 |
|
public function has($key) : bool |
106
|
|
|
{ |
107
|
1 |
|
return $this->doctrineCache->contains($key); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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: