Cache.php
4.19 KB
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
<?php
/**
* Cache.php
*
* The Cache class file.
*
* PHP versions 5
*
* @author Alexander Schneider <alexanderschneider85@gmail.com>
* @copyright 2008-2017 Alexander Schneider
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
* @version SVN: $id$
* @link http://wordpress.org/extend/plugins/user-access-manager/
*/
declare(strict_types=1);
namespace UserAccessManager\Cache;
use UserAccessManager\Wrapper\Wordpress;
/**
* Class Cache
*
* @package UserAccessManager\Cache
*/
class Cache
{
/**
* @var Wordpress
*/
private $wordpress;
/**
* @var CacheProviderFactory
*/
private $cacheProviderFactory;
/**
* @var null|CacheProviderInterface
*/
private $cacheProvider = null;
/**
* @var array
*/
private $cache = [];
/**
* @var array
*/
private $runtimeCache = [];
/**
* Cache constructor.
* @param Wordpress $wordpress
* @param CacheProviderFactory $cacheProviderFactory
*/
public function __construct(Wordpress $wordpress, CacheProviderFactory $cacheProviderFactory)
{
$this->wordpress = $wordpress;
$this->cacheProviderFactory = $cacheProviderFactory;
}
/**
* Return the cache provider.
* @return CacheProviderInterface|null
*/
public function getCacheProvider(): ?CacheProviderInterface
{
return $this->cacheProvider;
}
/**
* Sets a cache provider
* @param null|string $key
*/
public function setActiveCacheProvider(?string $key)
{
$cacheProviders = $this->getRegisteredCacheProviders();
$this->cacheProvider = (isset($cacheProviders[$key]) === true) ? $cacheProviders[$key] : null;
if ($this->cacheProvider !== null) {
$this->cacheProvider->init();
}
}
/**
* Returns a generated cache key.
* @return string
*/
public function generateCacheKey(): string
{
$arguments = func_get_args();
return implode('|', $arguments);
}
/**
* Adds the variable to the cache.
* @param string $key The cache key
* @param mixed $value The value.
*/
public function add(string $key, $value)
{
if ($this->cacheProvider !== null) {
$this->cacheProvider->add($key, $value);
}
$this->cache[$key] = $value;
}
/**
* Returns a value from the cache by the given key.
* @param string $key
* @return mixed
*/
public function get(string $key)
{
if (isset($this->cache[$key]) === false) {
$this->cache[$key] = ($this->cacheProvider !== null) ? $this->cacheProvider->get($key) : null;
}
return $this->cache[$key];
}
/**
* Invalidates the cached object.
* @param string $key
*/
public function invalidate(string $key)
{
if ($this->cacheProvider !== null) {
$this->cacheProvider->invalidate($key);
}
unset($this->cache[$key]);
}
/**
* Adds the variable to the runtime cache.
* @param string $key The cache key
* @param mixed $value The value.
*/
public function addToRuntimeCache(string $key, $value)
{
$this->runtimeCache[$key] = $value;
}
/**
* Returns a value from the runtime cache by the given key.
* @param string $key
* @return mixed
*/
public function getFromRuntimeCache(string $key)
{
if (isset($this->runtimeCache[$key]) === true) {
return $this->runtimeCache[$key];
}
return null;
}
/**
* Flushes the cache.
*/
public function flushCache()
{
$this->cache = [];
$this->runtimeCache = [];
}
/**
* Returns a list of the registered cache handlers.
* @return CacheProviderInterface[]
*/
public function getRegisteredCacheProviders(): array
{
$fileSystemCacheProvider = $this->cacheProviderFactory->createFileSystemCacheProvider();
return $this->wordpress->applyFilters(
'uam_registered_cache_handlers',
[$fileSystemCacheProvider->getId() => $fileSystemCacheProvider]
);
}
}