|
1
|
|
|
<?php |
|
2
|
|
|
namespace phpFastCache\Helper; |
|
3
|
|
|
|
|
4
|
|
|
use phpFastCache\CacheManager; |
|
5
|
|
|
use phpFastCache\Core\Item\ExtendedCacheItemInterface; |
|
6
|
|
|
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface; |
|
7
|
|
|
use phpFastCache\Exceptions\phpFastCacheDriverCheckException; |
|
8
|
|
|
use phpFastCache\Exceptions\phpFastCacheInvalidArgumentException; |
|
9
|
|
|
use phpFastCache\Exceptions\phpFastCacheRootException; |
|
10
|
|
|
use phpFastCache\Exceptions\phpFastCacheSimpleCacheException; |
|
11
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Psr16Adapter |
|
15
|
|
|
* @package phpFastCache\Helper |
|
16
|
|
|
*/ |
|
17
|
|
|
class Psr16Adapter implements CacheInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ExtendedCacheItemPoolInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $internalCacheInstance; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Psr16Adapter constructor. |
|
26
|
|
|
* @param $driver |
|
27
|
|
|
* @param array $config |
|
28
|
|
|
* @throws phpFastCacheDriverCheckException |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($driver, array $config = []) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->internalCacheInstance = CacheManager::getInstance($driver, $config); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $key |
|
37
|
|
|
* @param null $default |
|
38
|
|
|
* @return mixed|null |
|
39
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function get($key, $default = null) |
|
42
|
|
|
{ |
|
43
|
|
|
try { |
|
44
|
|
|
$cacheItemValue = $this->internalCacheInstance->getItem($key)->get(); |
|
45
|
|
|
if ($cacheItemValue !== null) { |
|
46
|
|
|
return $cacheItemValue; |
|
47
|
|
|
} else { |
|
48
|
|
|
return $default; |
|
49
|
|
|
} |
|
50
|
|
|
} catch (phpFastCacheInvalidArgumentException $e) { |
|
51
|
|
|
throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $key |
|
57
|
|
|
* @param mixed $value |
|
58
|
|
|
* @param null $ttl |
|
59
|
|
|
* @return bool |
|
60
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
|
61
|
|
|
*/ |
|
62
|
|
|
public function set($key, $value, $ttl = null) |
|
63
|
|
|
{ |
|
64
|
|
|
try { |
|
65
|
|
|
$cacheItem = $this->internalCacheInstance |
|
66
|
|
|
->getItem($key) |
|
67
|
|
|
->set($value); |
|
68
|
|
|
if (is_int($ttl) || $ttl instanceof \DateInterval) { |
|
69
|
|
|
$cacheItem->expiresAfter($ttl); |
|
70
|
|
|
} |
|
71
|
|
|
return $this->internalCacheInstance->save($cacheItem); |
|
72
|
|
|
} catch (phpFastCacheInvalidArgumentException $e) { |
|
73
|
|
|
throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $key |
|
79
|
|
|
* @return bool |
|
80
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
|
81
|
|
|
*/ |
|
82
|
|
View Code Duplication |
public function delete($key) |
|
83
|
|
|
{ |
|
84
|
|
|
try { |
|
85
|
|
|
return $this->internalCacheInstance->deleteItem($key); |
|
86
|
|
|
} catch (phpFastCacheInvalidArgumentException $e) { |
|
87
|
|
|
throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return bool |
|
93
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
|
94
|
|
|
*/ |
|
95
|
|
|
public function clear() |
|
96
|
|
|
{ |
|
97
|
|
|
try { |
|
98
|
|
|
return $this->internalCacheInstance->clear(); |
|
99
|
|
|
} catch (phpFastCacheRootException $e) { |
|
100
|
|
|
throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string[] $keys |
|
106
|
|
|
* @param null $default |
|
107
|
|
|
* @return array |
|
108
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getMultiple($keys, $default = null) |
|
111
|
|
|
{ |
|
112
|
|
|
try { |
|
113
|
|
|
return array_map(function (ExtendedCacheItemInterface $item) { |
|
|
|
|
|
|
114
|
|
|
return $item->get(); |
|
115
|
|
|
}, $this->internalCacheInstance->getItems($keys)); |
|
116
|
|
|
} catch (phpFastCacheInvalidArgumentException $e) { |
|
117
|
|
|
throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string[] $values |
|
123
|
|
|
* @param null|int|\DateInterval $ttl |
|
124
|
|
|
* @return bool |
|
125
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setMultiple($values, $ttl = null) |
|
128
|
|
|
{ |
|
129
|
|
|
try { |
|
130
|
|
|
foreach ($values as $key => $value) { |
|
131
|
|
|
$cacheItem = $this->internalCacheInstance->getItem($key)->set($value); |
|
132
|
|
|
$this->internalCacheInstance->saveDeferred($cacheItem); |
|
133
|
|
|
unset($cacheItem); |
|
134
|
|
|
} |
|
135
|
|
|
return $this->internalCacheInstance->commit(); |
|
136
|
|
|
} catch (phpFastCacheInvalidArgumentException $e) { |
|
137
|
|
|
throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param string[] $keys |
|
143
|
|
|
* @return bool |
|
144
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
|
145
|
|
|
*/ |
|
146
|
|
View Code Duplication |
public function deleteMultiple($keys) |
|
147
|
|
|
{ |
|
148
|
|
|
try { |
|
149
|
|
|
return $this->internalCacheInstance->deleteItems($keys); |
|
150
|
|
|
} catch (phpFastCacheInvalidArgumentException $e) { |
|
151
|
|
|
throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $key |
|
157
|
|
|
* @return bool |
|
158
|
|
|
* @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException |
|
159
|
|
|
*/ |
|
160
|
|
|
public function has($key) |
|
161
|
|
|
{ |
|
162
|
|
|
try { |
|
163
|
|
|
return $this->internalCacheInstance->getItem($key)->isHit(); |
|
164
|
|
|
} catch (phpFastCacheInvalidArgumentException $e) { |
|
165
|
|
|
throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.