-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache.php
234 lines (205 loc) · 5.62 KB
/
Cache.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
<?php
/**
* This file is part of the Vection package.
*
* (c) David M. Lung <vection@davidlung.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Vection\Component\Cache;
use Vection\Contracts\Cache\CacheInterface;
use Vection\Contracts\Cache\CacheProviderInterface;
/**
* Class Cache
*
* @package Vection\Component\Cache
*
* @author David Lung <vection@davidlung.de>
*/
class Cache implements CacheInterface
{
/**
* Each cache instance can have a specific key namespace
* to separate the values from other cache instances.
* This make it possible to manage different cache domains
* in multiple nested levels.
*
* @var string
*/
protected string $namespace;
/**
* A string that represents the separator between nested
* caching pools. E.g. when using the default separator ":"
* the namespaces would be "Vection-Cache:customPoolName:etc".
*
* @var string
*/
protected string $nsSeparator;
/**
* The specific cache provider. This cache class delegates
* all methods defined by CacheProviderInterface to this provider.
*
* @var CacheProviderInterface
*/
protected CacheProviderInterface $cacheProvider;
/**
* Pools are used for different Cache instances which can be
* managed in multiple nested levels. Each pool is a children
* Cache object of the Cache which has created the pool.
* Pools are aligned in a hierarchical structure.
*
* @var CacheInterface[]
*/
protected array $pools;
/**
* Cache constructor.
*
* @param CacheProviderInterface $cacheProvider
* @param string $namespace
* @param string $nsSeparator
*/
public function __construct(CacheProviderInterface $cacheProvider, string $namespace = '', string $nsSeparator = '')
{
$this->cacheProvider = $cacheProvider;
$this->namespace = $namespace ?: 'Vection-Cache';
$this->nsSeparator = $nsSeparator ?: ':';
}
/**
* @inheritdoc
*/
public function getNamespace(): string
{
return $this->namespace;
}
/**
* @inheritdoc
*/
public function getPool(string $namespace): CacheInterface
{
if ( ! isset($this->pools[$namespace]) ) {
# Create and save new Cache pool with extended pool namespace
$this->pools[$namespace] = new Cache(
$this->cacheProvider,
$this->namespace . $this->nsSeparator . $namespace,
$this->nsSeparator
);
}
return $this->pools[$namespace];
}
/**
* @inheritdoc
*/
public function clear(string $namespace = ''): bool
{
return $this->cacheProvider->clear($this->getNSKey($namespace));
}
/**
* @param string $key
*
* @return string
*/
protected function getNSKey(string $key): string
{
return $this->namespace . ( $key ? $this->nsSeparator . $key : '' );
}
/**
* @inheritdoc
*/
public function contains(string $key): bool
{
return $this->cacheProvider->contains($this->getNSKey($key));
}
/**
* @inheritdoc
*/
public function delete(string $key): bool
{
return $this->cacheProvider->delete($this->getNSKey($key));
}
/**
* @inheritdoc
*/
public function getString(string $key): ?string
{
return $this->cacheProvider->getString($this->getNSKey($key));
}
/**
* @inheritdoc
*/
public function getObject(string $key): ?object
{
return $this->cacheProvider->getObject($this->getNSKey($key));
}
/**
* @inheritdoc
*/
public function getArray(string $key): array|null
{
return $this->cacheProvider->getArray($this->getNSKey($key));
}
/**
* @inheritdoc
*/
public function getInt(string $key): ?int
{
return $this->cacheProvider->getInt($this->getNSKey($key));
}
/**
* @inheritdoc
*/
public function getFloat(string $key): ?float
{
return $this->cacheProvider->getFloat($this->getNSKey($key));
}
/**
* @inheritDoc
*/
public function get(string $key, mixed $default = null): mixed
{
return $this->cacheProvider->get($this->getNSKey($key), $default);
}
/**
* @inheritdoc
*/
public function setString(string $key, string $value, int $ttl = 0): bool
{
return $this->cacheProvider->setString($this->getNSKey($key), $value, $ttl);
}
/**
* @inheritdoc
*/
public function setObject(string $key, object $value, int $ttl = 0): bool
{
return $this->cacheProvider->setObject($this->getNSKey($key), $value, $ttl);
}
/**
* @inheritdoc
*/
public function setArray(string $key, array $value, int $ttl = 0): bool
{
return $this->cacheProvider->setArray($this->getNSKey($key), $value, $ttl);
}
/**
* @inheritdoc
*/
public function setInt(string $key, int $value, int $ttl = 0): bool
{
return $this->cacheProvider->setInt($this->getNSKey($key), $value, $ttl);
}
/**
* @inheritdoc
*/
public function setFloat(string $key, float $value, int $ttl = 0): bool
{
return $this->cacheProvider->setFloat($this->getNSKey($key), $value, $ttl);
}
/**
* @inheritDoc
*/
public function set(string $key, mixed $value, int $ttl = 0): bool
{
return $this->cacheProvider->set($this->getNSKey($key), $value, $ttl);
}
}