|
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 Psr\Cache\CacheItemPoolInterface; |
|
24
|
|
|
use TechDivision\Import\ConfigurationInterface; |
|
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 extends GenericCacheAdapter |
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The configuration instance. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \TechDivision\Import\ConfigurationInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $configuration; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Initialize the cache handler with the passed cache and configuration instances. |
|
47
|
|
|
* . |
|
48
|
|
|
* @param \Psr\Cache\CacheItemPoolInterface $cache The cache instance |
|
49
|
|
|
* @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(CacheItemPoolInterface $cache, ConfigurationInterface $configuration) |
|
52
|
|
|
{ |
|
53
|
|
|
|
|
54
|
|
|
// pass the cache adapter to the parent constructor |
|
55
|
|
|
parent::__construct($cache); |
|
56
|
|
|
|
|
57
|
|
|
// set the configuration instance |
|
58
|
|
|
$this->configuration = $configuration; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Query whether or not a cache value for the passed cache key is available. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $key The cache key to query for |
|
65
|
|
|
* |
|
66
|
|
|
* @return boolean TRUE if the a value is available, else FALSE |
|
67
|
|
|
*/ |
|
68
|
|
|
public function isCached($key) |
|
69
|
|
|
{ |
|
70
|
|
|
|
|
71
|
|
|
// query whether or not the item has been cached, and if yes if the cache is valid |
|
72
|
|
|
if ($this->configuration->isCacheEnabled()) { |
|
73
|
|
|
return parent::isCached($key); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// return FALSE in all other cases |
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Add the passed item to the cache. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $key The cache key to use |
|
84
|
|
|
* @param mixed $value The value that has to be cached |
|
85
|
|
|
* @param array $references An array with references to add |
|
86
|
|
|
* @param boolean $override Flag that allows to override an exising cache entry |
|
87
|
|
|
* |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
public function toCache($key, $value, array $references = array(), $override = false) |
|
91
|
|
|
{ |
|
92
|
|
|
|
|
93
|
|
|
// query whether or not the cache is enabled |
|
94
|
|
|
if ($this->configuration->isCacheEnabled()) { |
|
95
|
|
|
parent::toCache($key, $value, $references, $override); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|