Completed
Push — master ( ac9830...86bd1d )
by Aimeos
01:48
created

Flow::getMultiple()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2014-2016
6
 * @package MW
7
 * @subpackage Cache
8
 */
9
10
11
namespace Aimeos\MW\Cache;
12
13
14
/**
15
 * Flow caching implementation.
16
 *
17
 * @package MW
18
 * @subpackage Cache
19
 */
20
class Flow
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...
21
	extends \Aimeos\MW\Cache\Base
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
22
	implements \Aimeos\MW\Cache\Iface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
23
{
24
	private $object;
25
	private $prefix;
26
27
28
	/**
29
	 * Initializes the object instance.
30
	 *
31
	 * @param array $config List of configuration values
32
	 * @param \TYPO3\Flow\Cache\Frontend\FrontendInterface $cache TYPO3 cache object
33
	 */
34
	public function __construct( array $config, \TYPO3\Flow\Cache\Frontend\FrontendInterface $cache )
35
	{
36
		$this->prefix = ( isset( $config['siteid'] ) ? $config['siteid'] . '-' : '' );
37
		$this->object = $cache;
38
	}
39
40
41
	/**
42
	 * Removes the cache entry identified by the given key.
43
	 *
44
	 * @inheritDoc
45
	 *
46
	 * @param string $key Key string that identifies the single cache entry
47
	 */
48
	public function delete( $key )
49
	{
50
		$this->object->remove( $this->prefix . $key );
51
	}
52
53
54
	/**
55
	 * Removes the cache entries identified by the given keys.
56
	 *
57
	 * @inheritDoc
58
	 *
59
	 * @param \Traversable|array $keys List of key strings that identify the cache entries
60
	 * 	that should be removed
61
	 */
62
	public function deleteMultiple( $keys )
63
	{
64
		foreach( $keys as $key ) {
65
			$this->object->remove( $this->prefix . $key );
66
		}
67
	}
68
69
70
	/**
71
	 * Removes the cache entries identified by the given tags.
72
	 *
73
	 * @inheritDoc
74
	 *
75
	 * @param string[] $tags List of tag strings that are associated to one or more
76
	 * 	cache entries that should be removed
77
	 */
78
	public function deleteByTags( array $tags )
79
	{
80
		foreach( $tags as $tag ) {
81
			$this->object->flushByTag( $this->prefix . $tag );
82
		}
83
	}
84
85
86
	/**
87
	 * Removes all entries for the current site from the cache.
88
	 *
89
	 * @inheritDoc
90
	 */
91
	public function clear()
92
	{
93
		if( $this->prefix ) {
94
			$this->object->flushByTag( $this->prefix . 'siteid' );
95
		} else {
96
			$this->object->flush();
97
		}
98
	}
99
100
101
	/**
102
	 * Returns the value of the requested cache key.
103
	 *
104
	 * @inheritDoc
105
	 *
106
	 * @param string $name Path to the requested value like tree/node/classname
107
	 * @param string $default Value returned if requested key isn't found
108
	 * @return mixed Value associated to the requested key
109
	 */
110
	public function get( $name, $default = null )
111
	{
112
		if( ( $entry = $this->object->get( $this->prefix . $name ) ) !== false ) {
113
			return $entry;
114
		}
115
116
		return $default;
117
	}
118
119
120
	/**
121
	 * Returns the cached values for the given cache keys.
122
	 *
123
	 * @inheritDoc
124
	 *
125
	 * @param \Traversable|array $keys List of key strings for the requested cache entries
126
	 * @param mixed $default Default value to return for keys that do not exist
127
	 * @return array Associative list of key/value pairs for the requested cache
128
	 * 	entries. If a cache entry doesn't exist, neither its key nor a value
129
	 * 	will be in the result list
130
	 * @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond
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 $name Key string for the given value like product/id/123
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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 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( is_string( $expires ) ) {
196
			$expires = date_create( $expires )->getTimestamp() - time();
197
		}
198
199
		$tagList = ( $this->prefix ? array( $this->prefix . 'siteid' ) : array() );
200
201
		foreach( $tags as $tag ) {
202
			$tagList[] = $this->prefix . $tag;
203
		}
204
205
		$this->object->set( $this->prefix . $key, $value, $tagList, $expires );
206
	}
207
208
209
	/**
210
	 * Adds or overwrites the given key/value pairs in the cache, which is much
211
	 * more efficient than setting them one by one using the set() method.
212
	 *
213
	 * @inheritDoc
214
	 *
215
	 * @param \Traversable|array $pairs Associative list of key/value pairs. Both must be
216
	 * 	a string
217
	 * @param array|int|string|null $expires Associative list of keys and datetime
218
	 *  string or integer TTL pairs.
219
	 * @param array $tags Associative list of key/tag or key/tags pairs that
220
	 *  should be associated to the values identified by their key. The value
221
	 *  associated to the key can either be a tag string or an array of tag strings
222
	 * @return null
223
	 * @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond
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] : $expires );
231
232
			$this->set( $key, $value, $keyExpire, $tagList );
233
		}
234
	}
235
}
236