Passed
Push — master ( 5172db...50f410 )
by Aimeos
04:08
created

Standard::destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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