|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Cache\GenericCacheAdapter |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Cache; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\ConfigurationInterface; |
|
24
|
|
|
use TechDivision\Import\Utils\CacheTypes; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Configurable cache adapter implementation. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Tim Wagner <[email protected]> |
|
30
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
|
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
32
|
|
|
* @link https://github.com/techdivision/import |
|
33
|
|
|
* @link http://www.techdivision.com |
|
34
|
|
|
*/ |
|
35
|
|
|
class ConfigurableCacheAdapter implements CacheAdapterInterface |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The cache adatper instance. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \TechDivision\Import\Cache\CacheAdapterInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $cacheAdapter; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The TTL used to cache items. |
|
47
|
|
|
* |
|
48
|
|
|
* @var integer |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $ime = -1; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The flag if the cache is anabled or not. |
|
54
|
|
|
* |
|
55
|
|
|
* @var boolean |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $enabled = true; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Initialize the cache handler with the passed cache and configuration instances. |
|
61
|
|
|
* . |
|
62
|
|
|
* @param \TechDivision\Import\Cache\CacheAdapterInterface $cacheAdapter The cache instance |
|
63
|
|
|
* @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance |
|
64
|
|
|
* @param string $type The cache type to use |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct( |
|
67
|
|
|
CacheAdapterInterface $cacheAdapter, |
|
68
|
|
|
ConfigurationInterface $configuration, |
|
69
|
|
|
$type = CacheTypes::TYPE_STATIC |
|
70
|
|
|
) { |
|
71
|
|
|
|
|
72
|
|
|
// load the configuration for the passed cache type |
|
73
|
|
|
$cacheConfiguration = $configuration->getCacheByType($type); |
|
74
|
|
|
|
|
75
|
|
|
// initialize the ttl and the enabled flag |
|
76
|
|
|
$this->time = $cacheConfiguration->getTime(); |
|
|
|
|
|
|
77
|
|
|
$this->enabled = $cacheConfiguration->isEnabled(); |
|
78
|
|
|
|
|
79
|
|
|
// set the cache adapter to use |
|
80
|
|
|
$this->cacheAdapter = $cacheAdapter; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Creates a unique cache key from the passed data. |
|
85
|
|
|
* |
|
86
|
|
|
* @param mixed $data The date to create the cache key from |
|
87
|
|
|
* |
|
88
|
|
|
* @return string The generated cache key |
|
89
|
|
|
*/ |
|
90
|
|
|
public function cacheKey($data) |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->cacheAdapter->cacheKey($data); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Inversion of the isCached() method. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $key The cache key to query for |
|
99
|
|
|
* |
|
100
|
|
|
* @return boolean TRUE if the value is not available, else FALSE |
|
101
|
|
|
*/ |
|
102
|
|
|
public function notCached($key) |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->cacheAdapter->notCached($key); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Add's a cache reference from one key to another. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $from The key to reference from |
|
111
|
|
|
* @param string $to The key to reference to |
|
112
|
|
|
* |
|
113
|
|
|
* @return void |
|
114
|
|
|
*/ |
|
115
|
|
|
public function addReference($from, $to) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->cacheAdapter->addReference($from, $to); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Returns a new cache item for the passed key |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $key The cache key to return the item for |
|
124
|
|
|
* |
|
125
|
|
|
* @return mixed The value for the passed key |
|
126
|
|
|
*/ |
|
127
|
|
|
public function fromCache($key) |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->cacheAdapter->fromCache($key); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Flush the cache and remove the references. |
|
134
|
|
|
* |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
|
|
public function flushCache() |
|
138
|
|
|
{ |
|
139
|
|
|
$this->cacheAdapter->flushCache(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Remove the item with the passed key and all its references from the cache. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $key The key of the cache item to Remove |
|
146
|
|
|
* |
|
147
|
|
|
* @return void |
|
148
|
|
|
*/ |
|
149
|
|
|
public function removeCache($key) |
|
150
|
|
|
{ |
|
151
|
|
|
$this->cacheAdapter->removeCache($key); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Raises the value for the attribute with the passed key by one. |
|
156
|
|
|
* |
|
157
|
|
|
* @param mixed $key The key of the attribute to raise the value for |
|
158
|
|
|
* @param mixed $counterName The name of the counter to raise |
|
159
|
|
|
* |
|
160
|
|
|
* @return integer The counter's new value |
|
161
|
|
|
*/ |
|
162
|
|
|
public function raiseCounter($key, $counterName) |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->cacheAdapter->raiseCounter($key, $counterName); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* This method merges the passed attributes with an array that |
|
169
|
|
|
* has already been added under the passed key. |
|
170
|
|
|
* |
|
171
|
|
|
* If no value will be found under the passed key, the attributes |
|
172
|
|
|
* will simply be registered. |
|
173
|
|
|
* |
|
174
|
|
|
* @param mixed $key The key of the attributes that has to be merged with the passed ones |
|
175
|
|
|
* @param array $attributes The attributes that has to be merged with the exising ones |
|
176
|
|
|
* |
|
177
|
|
|
* @return void |
|
178
|
|
|
* @throws \Exception Is thrown, if the already registered value is no array |
|
179
|
|
|
* @link http://php.net/array_replace_recursive |
|
180
|
|
|
*/ |
|
181
|
|
|
public function mergeAttributesRecursive($key, array $attributes) |
|
182
|
|
|
{ |
|
183
|
|
|
$this->cacheAdapter->mergeAttributesRecursive($key, $attributes); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Query whether or not a cache value for the passed cache key is available. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $key The cache key to query for |
|
190
|
|
|
* |
|
191
|
|
|
* @return boolean TRUE if the a value is available, else FALSE |
|
192
|
|
|
*/ |
|
193
|
|
|
public function isCached($key) |
|
194
|
|
|
{ |
|
195
|
|
|
|
|
196
|
|
|
// query whether or not the item has been cached, and if yes if the cache is valid |
|
197
|
|
|
if ($this->enabled) { |
|
198
|
|
|
return $this->cacheAdapter->isCached($key); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
// return FALSE in all other cases |
|
202
|
|
|
return false; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Add the passed item to the cache. |
|
207
|
|
|
* |
|
208
|
|
|
* @param string $key The cache key to use |
|
209
|
|
|
* @param mixed $value The value that has to be cached |
|
210
|
|
|
* @param array $references An array with references to add |
|
211
|
|
|
* @param boolean $override Flag that allows to override an exising cache entry |
|
212
|
|
|
* @param integer $time The TTL in seconds for the passed item |
|
213
|
|
|
* |
|
214
|
|
|
* @return void |
|
215
|
|
|
*/ |
|
216
|
|
|
public function toCache($key, $value, array $references = array(), $override = false, $time = null) |
|
217
|
|
|
{ |
|
218
|
|
|
|
|
219
|
|
|
// query whether or not the cache is enabled |
|
220
|
|
|
if ($this->enabled) { |
|
221
|
|
|
$this->cacheAdapter->toCache($key, $value, $references, $override, $time ? $time : $this->time); |
|
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: