Passed
Push — master ( 0cda2e...f3fa1e )
by Aimeos
17:16 queued 07:34
created

Standard::setMultiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 * @package MW
8
 * @subpackage MAdmin
9
 */
10
11
12
namespace Aimeos\MAdmin\Cache\Proxy;
13
14
15
/**
16
 * Cache proxy for creating cache object on demand.
17
 *
18
 * @package MAdmin
19
 * @subpackage Cache
20
 */
21
class Standard
22
	implements \Aimeos\MW\Cache\Iface
23
{
24
	private $object;
25
	private $context;
26
27
28
	/**
29
	 * Initializes the cache controller.
30
	 *
31
	 * @param \Aimeos\MShop\Context\Item\Iface $context MShop context object
32
	 */
33
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context )
34
	{
35
		$this->context = $context;
36
	}
37
38
39
	/**
40
	 * Removes all expired cache entries.
41
	 *
42
	 * @inheritDoc
43
	 *
44
	 * @return bool True on success and false on failure
45
	 */
46
	public function cleanup() : bool
47
	{
48
		return $this->object()->cleanup();
49
	}
50
51
52
	/**
53
	 * Removes all entries of the site from the cache.
54
	 *
55
	 * @inheritDoc
56
	 *
57
	 * @return bool True on success and false on failure
58
	 */
59
	public function clear() : bool
60
	{
61
		return $this->object()->clear();
62
	}
63
64
65
	/**
66
	 * Removes the cache entry identified by the given key.
67
	 *
68
	 * @inheritDoc
69
	 *
70
	 * @param string $key Key string that identifies the single cache entry
71
	 * @return bool True if the item was successfully removed. False if there was an error
72
	 * @throws \Psr\SimpleCache\InvalidArgumentException
73
	 */
74
	public function delete( string $key ) : bool
75
	{
76
		return $this->object()->delete( $key );
77
	}
78
79
80
	/**
81
	 * Removes the cache entries identified by the given keys.
82
	 *
83
	 * @inheritDoc
84
	 *
85
	 * @param iterable $keys List of key strings that identify the cache entries that should be removed
86
	 * @return bool True if the items were successfully removed. False if there was an error.
87
	 * @throws \Psr\SimpleCache\InvalidArgumentException
88
	 */
89
	public function deleteMultiple( iterable $keys ) : bool
90
	{
91
		return $this->object()->deleteMultiple( $keys );
92
	}
93
94
95
	/**
96
	 * Removes the cache entries identified by the given tags.
97
	 *
98
	 * @inheritDoc
99
	 *
100
	 * @param iterable $tags List of tag strings that are associated to one or
101
	 *  more cache entries that should be removed
102
	 * @return bool True if the items were successfully removed. False if there was an error.
103
	 * @throws \Psr\SimpleCache\InvalidArgumentException
104
	 */
105
	public function deleteByTags( iterable $tags ) : bool
106
	{
107
		return $this->object()->deleteByTags( $tags );
108
	}
109
110
111
	/**
112
	 * Returns the cached value for the given key.
113
	 *
114
	 * @inheritDoc
115
	 *
116
	 * @param string $key Path to the requested value like product/id/123
117
	 * @param mixed $default Value returned if requested key isn't found
118
	 * @return mixed Value associated to the requested key. If no value for the
119
	 *	key is found in the cache, the given default value is returned
120
	 * @throws \Psr\SimpleCache\InvalidArgumentException
121
	 */
122
	public function get( string $key, $default = null )
123
	{
124
		return $this->object()->get( $key, $default );
125
	}
126
127
128
	/**
129
	 * Returns the cached values for the given cache keys if available.
130
	 *
131
	 * @inheritDoc
132
	 *
133
	 * @param iterable $keys List of key strings for the requested cache entries
134
	 * @param mixed $default Default value to return for keys that do not exist
135
	 * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
136
	 * @throws \Psr\SimpleCache\InvalidArgumentException
137
	 */
138
	public function getMultiple( iterable $keys, $default = null ) : iterable
139
	{
140
		return $this->object()->getMultiple( $keys, $default );
141
	}
142
143
144
	/**
145
	 * Determines whether an item is present in the cache.
146
	 *
147
	 * @inheritDoc
148
	 *
149
	 * @param string $key The cache item key
150
	 * @return bool True if cache entry is available, false if not
151
	 * @throws \Psr\SimpleCache\InvalidArgumentException
152
	 */
153
	public function has( string $key ) : bool
154
	{
155
		return $this->object()->has( $key );
156
	}
157
158
159
	/**
160
	 * Sets the value for the given key in the cache.
161
	 *
162
	 * @inheritDoc
163
	 *
164
	 * @param string $key Key string for the given value like product/id/123
165
	 * @param mixed $value Value string that should be stored for the given key
166
	 * @param \DateInterval|int|string|null $expires Date interval object,
167
	 *  date/time string in "YYYY-MM-DD HH:mm:ss" format or as integer TTL value
168
	 *  when the cache entry will expiry
169
	 * @param iterable $tags List of tag strings that should be assoicated to the cache entry
170
	 * @return bool True on success and false on failure.
171
	 * @throws \Psr\SimpleCache\InvalidArgumentException
172
	 */
173
	public function set( string $key, $value, $expires = null, iterable $tags = [] ) : bool
174
	{
175
		return $this->object()->set( $key, $value, $expires, $tags );
176
	}
177
178
179
	/**
180
	 * Adds or overwrites the given key/value pairs in the cache, which is much
181
	 * more efficient than setting them one by one using the set() method.
182
	 *
183
	 * @inheritDoc
184
	 *
185
	 * @param iterable $pairs Associative list of key/value pairs. Both must be a string
186
	 * @param \DateInterval|int|string|null $expires Date interval object,
187
	 *  date/time string in "YYYY-MM-DD HH:mm:ss" format or as integer TTL value
188
	 *  when the cache entry will expiry
189
	 * @param iterable $tags List of tags that should be associated to the cache entries
190
	 * @return bool True on success and false on failure.
191
	 * @throws \Psr\SimpleCache\InvalidArgumentException
192
	 */
193
	public function setMultiple( iterable $pairs, $expires = null, iterable $tags = [] ) : bool
194
	{
195
		return $this->object()->setMultiple( $pairs, $expires, $tags );
196
	}
197
198
199
	/**
200
	 * Returns the cache object or creates a new one if it doesn't exist yet.
201
	 *
202
	 * @return \Aimeos\MW\Cache\Iface Cache object
203
	 */
204
	protected function object()
205
	{
206
		if( !isset( $this->object ) ) {
207
			$this->object = \Aimeos\MAdmin::create( $this->context, 'cache' )->getCache();
208
		}
209
210
		return $this->object;
211
	}
212
}
213