This repository has been archived by the owner on Jul 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Flow.php
235 lines (200 loc) · 5.81 KB
/
Flow.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
/**
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
* @copyright Aimeos (aimeos.org), 2014-2016
* @package MW
* @subpackage Cache
*/
namespace Aimeos\MW\Cache;
/**
* Flow caching implementation.
*
* @package MW
* @subpackage Cache
*/
class Flow
extends \Aimeos\MW\Cache\Base
implements \Aimeos\MW\Cache\Iface
{
private $object;
private $prefix;
/**
* Initializes the object instance.
*
* @param array $config List of configuration values
* @param \TYPO3\Flow\Cache\Frontend\FrontendInterface $cache TYPO3 cache object
*/
public function __construct( array $config, \TYPO3\Flow\Cache\Frontend\FrontendInterface $cache )
{
$this->prefix = ( isset( $config['siteid'] ) ? $config['siteid'] . '-' : '' );
$this->object = $cache;
}
/**
* Removes the cache entry identified by the given key.
*
* @inheritDoc
*
* @param string $key Key string that identifies the single cache entry
*/
public function delete( $key )
{
$this->object->remove( $this->prefix . $key );
}
/**
* Removes the cache entries identified by the given keys.
*
* @inheritDoc
*
* @param \Traversable|array $keys List of key strings that identify the cache entries
* that should be removed
*/
public function deleteMultiple( $keys )
{
foreach( $keys as $key ) {
$this->object->remove( $this->prefix . $key );
}
}
/**
* Removes the cache entries identified by the given tags.
*
* @inheritDoc
*
* @param string[] $tags List of tag strings that are associated to one or more
* cache entries that should be removed
*/
public function deleteByTags( array $tags )
{
foreach( $tags as $tag ) {
$this->object->flushByTag( $this->prefix . $tag );
}
}
/**
* Removes all entries for the current site from the cache.
*
* @inheritDoc
*/
public function clear()
{
if( $this->prefix ) {
$this->object->flushByTag( $this->prefix . 'siteid' );
} else {
$this->object->flush();
}
}
/**
* Returns the value of the requested cache key.
*
* @inheritDoc
*
* @param string $name Path to the requested value like tree/node/classname
* @param string $default Value returned if requested key isn't found
* @return mixed Value associated to the requested key
*/
public function get( $name, $default = null )
{
if( ( $entry = $this->object->get( $this->prefix . $name ) ) !== false ) {
return $entry;
}
return $default;
}
/**
* Returns the cached values for the given cache keys.
*
* @inheritDoc
*
* @param \Traversable|array $keys List of key strings for the requested cache entries
* @param mixed $default Default value to return for keys that do not exist
* @return array Associative list of key/value pairs for the requested cache
* entries. If a cache entry doesn't exist, neither its key nor a value
* will be in the result list
* @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond
*/
public function getMultiple( $keys, $default = null )
{
$result = array();
foreach( $keys as $key )
{
if( ( $entry = $this->object->get( $this->prefix . $key ) ) !== false ) {
$result[$key] = $entry;
} else {
$result[$key] = $default;
}
}
return $result;
}
/**
* Returns the cached keys and values associated to the given tags.
*
* @inheritDoc
*
* @param string[] $tags List of tag strings associated to the requested cache entries
* @return array Associative list of key/value pairs for the requested cache
* entries. If a tag isn't associated to any cache entry, nothing is returned
* for that tag
*/
public function getMultipleByTags( array $tags )
{
$result = array();
$len = strlen( $this->prefix );
foreach( $tags as $tag )
{
foreach( $this->object->getByTag( $this->prefix . $tag ) as $key => $value )
{
if( strncmp( $key, $this->prefix, $len ) === 0 ) {
$result[ substr( $key, $len ) ] = $value;
} else {
$result[$key] = $value;
}
}
}
return $result;
}
/**
* Sets the value for the given key in the cache.
*
* @inheritDoc
*
* @param string $key Key string for the given value like product/id/123
* @param mixed $value Value string that should be stored for the given key
* @param int|string|null $expires Date/time string in "YYYY-MM-DD HH:mm:ss"
* format when the cache entry expires
* @param array $tags List of tag strings that should be assoicated to the
* given value in the cache
*/
public function set( $key, $value, $expires = null, array $tags = array() )
{
if( is_string( $expires ) ) {
$expires = date_create( $expires )->getTimestamp() - time();
}
$tagList = ( $this->prefix ? array( $this->prefix . 'siteid' ) : array() );
foreach( $tags as $tag ) {
$tagList[] = $this->prefix . $tag;
}
$this->object->set( $this->prefix . $key, $value, $tagList, $expires );
}
/**
* Adds or overwrites the given key/value pairs in the cache, which is much
* more efficient than setting them one by one using the set() method.
*
* @inheritDoc
*
* @param \Traversable|array $pairs Associative list of key/value pairs. Both must be
* a string
* @param array|int|string|null $expires Associative list of keys and datetime
* string or integer TTL pairs.
* @param array $tags Associative list of key/tag or key/tags pairs that
* should be associated to the values identified by their key. The value
* associated to the key can either be a tag string or an array of tag strings
* @return null
* @throws \Aimeos\MW\Cache\Exception If the cache server doesn't respond
*/
public function setMultiple( $pairs, $expires = null, array $tags = array() )
{
foreach( $pairs as $key => $value )
{
$tagList = ( isset( $tags[$key] ) ? (array) $tags[$key] : array() );
$keyExpire = ( isset( $expires[$key] ) ? $expires[$key] : $expires );
$this->set( $key, $value, $keyExpire, $tagList );
}
}
}