Passed
Push — master ( 6e9653...bf8feb )
by Aimeos
13:41 queued 11:14
created

Typo3::cleanup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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