Completed
Push — master ( 06e841...fb9ef9 )
by Aimeos
01:39
created

Typo3::getList()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
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-2016
7
 * @package MW
8
 * @subpackage Cache
9
 */
10
11
12
namespace Aimeos\MW\Cache;
13
14
15
/**
16
 * TYPO3 caching implementation.
17
 *
18
 * @package MW
19
 * @subpackage Cache
20
 */
21
class Typo3
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: deleteList, flush, getList, getListByTags, setList
Loading history...
22
	extends \Aimeos\MW\Cache\Base
23
	implements \Aimeos\MW\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
	 */
35
	public function __construct( array $config, \TYPO3\CMS\Core\Cache\Frontend\FrontendInterface $cache )
36
	{
37
		$this->prefix = ( isset( $config['siteid'] ) ? $config['siteid'] . '-' : '' );
38
		$this->object = $cache;
39
	}
40
41
42
	/**
43
	 * Removes the cache entry identified by the given key.
44
	 *
45
	 * @inheritDoc
46
	 *
47
	 * @param string $key Key string that identifies the single cache entry
48
	 */
49
	public function delete( $key )
50
	{
51
		$this->object->remove( $this->prefix . $key );
52
	}
53
54
55
	/**
56
	 * Removes the cache entries identified by the given keys.
57
	 *
58
	 * @inheritDoc
59
	 *
60
	 * @param iterable $keys List of key strings that identify the cache entries
61
	 * 	that should be removed
62
	 */
63
	public function deleteMultiple( $keys )
64
	{
65
		foreach( $keys as $key ) {
66
			$this->object->remove( $this->prefix . $key );
67
		}
68
	}
69
70
71
	/**
72
	 * Removes the cache entries identified by the given tags.
73
	 *
74
	 * @inheritDoc
75
	 *
76
	 * @param string[] $tags List of tag strings that are associated to one or more
77
	 * 	cache entries that should be removed
78
	 */
79
	public function deleteByTags( array $tags )
80
	{
81
		foreach( $tags as $tag ) {
82
			$this->object->flushByTag( $this->prefix . $tag );
83
		}
84
	}
85
86
87
	/**
88
	 * Removes all entries for the current site from the cache.
89
	 *
90
	 * @inheritDoc
91
	 */
92
	public function clear()
93
	{
94
		if( $this->prefix ) {
95
			$this->object->flushByTag( $this->prefix . 'siteid' );
96
		} else {
97
			$this->object->flush();
98
		}
99
	}
100
101
102
	/**
103
	 * Returns the value of the requested cache key.
104
	 *
105
	 * @inheritDoc
106
	 *
107
	 * @param string $name Path to the requested value like tree/node/classname
108
	 * @param string $default Value returned if requested key isn't found
109
	 * @return mixed Value associated to the requested key
110
	 */
111
	public function get( $name, $default = null )
112
	{
113
		if( ( $entry = $this->object->get( $this->prefix . $name ) ) !== false ) {
114
			return $entry;
115
		}
116
117
		return $default;
118
	}
119
120
121
	/**
122
	 * Returns the cached values for the given cache keys.
123
	 *
124
	 * @inheritDoc
125
	 *
126
	 * @param iterable $keys List of key strings for the requested cache entries
127
	 * @param mixed $default Default value to return for keys that do not exist
128
	 * @return array Associative list of key/value pairs for the requested cache
129
	 * 	entries. If a cache entry doesn't exist, neither its key nor a value
130
	 * 	will be in the result list
131
	 */
132
	public function getMultiple( $keys, $default = null )
133
	{
134
		$result = array();
135
136
		foreach( $keys as $key )
137
		{
138
			if( ( $entry = $this->object->get( $this->prefix . $key ) ) !== false ) {
139
				$result[$key] = $entry;
140
			} else {
141
				$result[$key] = $default;
142
			}
143
		}
144
145
		return $result;
146
	}
147
148
149
	/**
150
	 * Returns the cached keys and values associated to the given tags.
151
	 *
152
	 * @inheritDoc
153
	 *
154
	 * @param string[] $tags List of tag strings associated to the requested cache entries
155
	 * @return array Associative list of key/value pairs for the requested cache
156
	 * 	entries. If a tag isn't associated to any cache entry, nothing is returned
157
	 * 	for that tag
158
	 */
159
	public function getMultipleByTags( array $tags )
160
	{
161
		$result = array();
162
		$len = strlen( $this->prefix );
163
164
		foreach( $tags as $tag )
165
		{
166
			foreach( $this->object->getByTag( $this->prefix . $tag ) as $key => $value )
167
			{
168
				if( strncmp( $key, $this->prefix, $len ) === 0 ) {
169
					$result[ substr( $key, $len ) ] = $value;
170
				} else {
171
					$result[$key] = $value;
172
				}
173
174
			}
175
		}
176
177
		return $result;
178
	}
179
180
181
	/**
182
	 * Sets the value for the given key in the cache.
183
	 *
184
	 * @inheritDoc
185
	 *
186
	 * @param string $key Key string for the given value like product/id/123
187
	 * @param mixed $value Value string that should be stored for the given key
188
	 * @param int|string|null $expires Date/time string in "YYYY-MM-DD HH:mm:ss"
189
	 * 	format or as TTL value when the cache entry expires
190
	 * @param array $tags List of tag strings that should be assoicated to the
191
	 * 	given value in the cache
192
	 */
193
	public function set( $key, $value, $expires = null, array $tags = array() )
194
	{
195
		if( $expires !== null && ( $timestamp = strtotime( $expires ) ) !== false ) {
196
			$expires = $timestamp;
197
		} else {
198
			$expires = null;
199
		}
200
201
		$tagList = ( $this->prefix ? array( $this->prefix . 'siteid' ) : array() );
202
203
		foreach( $tags as $tag ) {
204
			$tagList[] = $this->prefix . $tag;
205
		}
206
207
		$this->object->set( $this->prefix . $key, $value, $tagList, $expires );
208
	}
209
210
211
	/**
212
	 * Adds or overwrites the given key/value pairs in the cache, which is much
213
	 * more efficient than setting them one by one using the set() method.
214
	 *
215
	 * @inheritDoc
216
	 *
217
	 * @param iterable $pairs Associative list of key/value pairs. Both must be
218
	 * 	a string
219
	 * @param int|string|array $expires Associative list of keys and datetime
220
	 *  string or integer TTL pairs.
221
	 * @param array $tags Associative list of key/tag or key/tags pairs that
222
	 *  should be associated to the values identified by their key. The value
223
	 *  associated to the key can either be a tag string or an array of tag strings
224
	 */
225
	public function setMultiple( $pairs, $expires = null, array $tags = array() )
226
	{
227
		foreach( $pairs as $key => $value )
228
		{
229
			$tagList = ( isset( $tags[$key] ) ? (array) $tags[$key] : array() );
230
			$keyExpire = ( isset( $expires[$key] ) ? $expires[$key] : null );
231
232
			$this->set( $key, $value, $keyExpire, $tagList );
233
		}
234
	}
235
}
236