Typo3::set()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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