1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
5 | * @copyright Aimeos (aimeos.org), 2015-2024 |
||
6 | * @package Base |
||
7 | * @subpackage Cache |
||
8 | */ |
||
9 | |||
10 | |||
11 | namespace Aimeos\Base\Cache; |
||
12 | |||
13 | |||
14 | /** |
||
15 | * Laravel caching implementation. |
||
16 | * |
||
17 | * @package Base |
||
18 | * @subpackage Cache |
||
19 | */ |
||
20 | class Laravel |
||
21 | extends \Aimeos\Base\Cache\Base |
||
22 | implements \Aimeos\Base\Cache\Iface |
||
23 | { |
||
24 | private \Illuminate\Contracts\Cache\Store $object; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * Initializes the object instance. |
||
29 | * |
||
30 | * @param \Illuminate\Contracts\Cache\Store $cache Laravel cache object |
||
31 | */ |
||
32 | public function __construct( \Illuminate\Contracts\Cache\Store $cache ) |
||
33 | { |
||
34 | $this->object = $cache; |
||
35 | } |
||
36 | |||
37 | |||
38 | /** |
||
39 | * Removes all entries from the cache so it's completely empty. |
||
40 | * |
||
41 | * @inheritDoc |
||
42 | * |
||
43 | * This method deletes all cached entries from the cache server the client |
||
44 | * has access to. This method is primarily usefull to provide a clean start |
||
45 | * before new entries are added to the cache and you don't know which |
||
46 | * entries are still in the cache. |
||
47 | * |
||
48 | * @return bool True on success and false on failure |
||
49 | */ |
||
50 | public function clear() : bool |
||
51 | { |
||
52 | return $this->object->flush(); |
||
53 | } |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Removes the cache entry identified by the given key. |
||
58 | * |
||
59 | * @inheritDoc |
||
60 | * |
||
61 | * @param string $key Key string that identifies the single cache entry |
||
62 | * @return bool True if the item was successfully removed. False if there was an error |
||
63 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
64 | */ |
||
65 | public function delete( string $key ) : bool |
||
66 | { |
||
67 | return $this->object->forget( $key ); |
||
68 | } |
||
69 | |||
70 | |||
71 | /** |
||
72 | * Removes the cache entries identified by the given keys. |
||
73 | * |
||
74 | * @inheritDoc |
||
75 | * |
||
76 | * @param iterable $keys List of key strings that identify the cache entries that should be removed |
||
77 | * @return bool True if the items were successfully removed. False if there was an error. |
||
78 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
79 | */ |
||
80 | public function deleteMultiple( iterable $keys ) : bool |
||
81 | { |
||
82 | foreach( $keys as $key ) { |
||
83 | $this->object->forget( $key ); |
||
84 | } |
||
85 | |||
86 | return true; |
||
87 | } |
||
88 | |||
89 | |||
90 | /** |
||
91 | * Removes the cache entries identified by the given tags. |
||
92 | * |
||
93 | * @inheritDoc |
||
94 | * |
||
95 | * @param iterable $tags List of tag strings that are associated to one or more cache entries that should be removed |
||
96 | * @return bool True if the items were successfully removed. False if there was an error. |
||
97 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
98 | */ |
||
99 | public function deleteByTags( iterable $tags ) : bool |
||
100 | { |
||
101 | return $this->object->flush(); |
||
102 | } |
||
103 | |||
104 | |||
105 | /** |
||
106 | * Returns the value of the requested cache key. |
||
107 | * |
||
108 | * @inheritDoc |
||
109 | * |
||
110 | * @param string $key Path to the requested value like product/id/123 |
||
111 | * @param mixed $default Value returned if requested key isn't found |
||
112 | * @return mixed Value associated to the requested key. If no value for the |
||
113 | * key is found in the cache, the given default value is returned |
||
114 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
115 | */ |
||
116 | public function get( string $key, $default = null ) |
||
117 | { |
||
118 | if( ( $entry = $this->object->get( $key ) ) !== null ) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
Are you sure the assignment to
$entry is correct as $this->object->get($key) targeting Illuminate\Cache\NullStore::get() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.
Loading history...
|
|||
119 | return $entry; |
||
120 | } |
||
121 | |||
122 | return $default; |
||
123 | } |
||
124 | |||
125 | |||
126 | /** |
||
127 | * Returns the cached values for the given cache keys. |
||
128 | * |
||
129 | * @inheritDoc |
||
130 | * |
||
131 | * @param iterable $keys List of key strings for the requested cache entries |
||
132 | * @param mixed $default Default value to return for keys that do not exist |
||
133 | * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value. |
||
134 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
135 | */ |
||
136 | public function getMultiple( iterable $keys, $default = null ) : iterable |
||
137 | { |
||
138 | $result = []; |
||
139 | |||
140 | foreach( $keys as $key ) |
||
141 | { |
||
142 | if( ( $entry = $this->object->get( $key ) ) !== false ) { |
||
0 ignored issues
–
show
Are you sure the assignment to
$entry is correct as $this->object->get($key) targeting Illuminate\Cache\NullStore::get() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.
Loading history...
|
|||
143 | $result[$key] = $entry; |
||
144 | } else { |
||
145 | $result[$key] = $default; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | return $result; |
||
150 | } |
||
151 | |||
152 | |||
153 | /** |
||
154 | * Determines whether an item is present in the cache. |
||
155 | * |
||
156 | * @inheritDoc |
||
157 | * |
||
158 | * @param string $key The cache item key |
||
159 | * @return bool True if cache entry is available, false if not |
||
160 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
161 | */ |
||
162 | public function has( string $key ) : bool |
||
163 | { |
||
164 | if( $this->object->get( $key ) !== null ) { |
||
0 ignored issues
–
show
Are you sure the usage of
$this->object->get($key) targeting Illuminate\Cache\NullStore::get() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.
Loading history...
|
|||
165 | return true; |
||
166 | } |
||
167 | |||
168 | return false; |
||
169 | } |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Sets the value for the specified key. |
||
174 | * |
||
175 | * @inheritDoc |
||
176 | * |
||
177 | * @param string $key Key string for the given value like product/id/123 |
||
178 | * @param mixed $value Value string that should be stored for the given key |
||
179 | * @param \DateInterval|int|string|null $expires Date interval object, |
||
180 | * date/time string in "YYYY-MM-DD HH:mm:ss" format or as integer TTL value |
||
181 | * when the cache entry will expiry |
||
182 | * @param iterable $tags List of tag strings that should be assoicated to the cache entry |
||
183 | * @return bool True on success and false on failure. |
||
184 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
185 | */ |
||
186 | public function set( string $key, $value, $expires = null, iterable $tags = [] ) : bool |
||
187 | { |
||
188 | if( $expires instanceof \DateInterval ) { |
||
189 | return $this->object->put( $key, $value, (int) ( date_create()->add( $expires )->format( 'Y-m-d H:i:s' ) - time() ) ); |
||
190 | } elseif( is_string( $expires ) ) { |
||
191 | return $this->object->put( $key, $value, (int) ( date_create( $expires )->getTimestamp() - time() ) ); |
||
192 | } elseif( is_int( $expires ) ) { |
||
193 | return $this->object->put( $key, $value, (int) $expires ); |
||
194 | } else { |
||
195 | return $this->object->forever( $key, $value ); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | |||
200 | /** |
||
201 | * Adds the given key/value pairs to the cache. |
||
202 | * |
||
203 | * @inheritDoc |
||
204 | * |
||
205 | * @param iterable $pairs Associative list of key/value pairs. Both must be a string |
||
206 | * @param \DateInterval|int|string|null $expires Date interval object, |
||
207 | * date/time string in "YYYY-MM-DD HH:mm:ss" format or as integer TTL value |
||
208 | * when the cache entry will expiry |
||
209 | * @param iterable $tags List of tags that should be associated to the cache entries |
||
210 | * @return bool True on success and false on failure. |
||
211 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
212 | */ |
||
213 | public function setMultiple( iterable $pairs, $expires = null, iterable $tags = [] ) : bool |
||
214 | { |
||
215 | foreach( $pairs as $key => $value ) { |
||
216 | $this->set( $key, $value, $expires, $tags ); |
||
217 | } |
||
218 | |||
219 | return true; |
||
220 | } |
||
221 | } |
||
222 |