|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Cache; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* PrefixedCache decorates any PSR-16 cache to add global prefix. It is added to every cache key so that it is unique |
|
11
|
|
|
* globally in the whole cache storage. It is recommended that you set a unique cache key prefix for each application |
|
12
|
|
|
* if the same cache storage is being used by different applications. |
|
13
|
|
|
* |
|
14
|
|
|
* ```php |
|
15
|
|
|
* $cache = new PrefixedCache(new ArrayCache(), 'my_app_'); |
|
16
|
|
|
* $cache->set('answer', 42); // Will set 42 to my_app_answer key. |
|
17
|
|
|
* ``` |
|
18
|
|
|
*/ |
|
19
|
|
|
final class PrefixedCache implements CacheInterface |
|
20
|
|
|
{ |
|
21
|
|
|
private CacheInterface $cache; |
|
22
|
|
|
private string $prefix; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param CacheInterface $cache PSR-16 cache to add prefix to. |
|
26
|
|
|
* @param string $prefix Prefix to use for all cache keys. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(CacheInterface $cache, string $prefix) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->cache = $cache; |
|
31
|
|
|
$this->prefix = $prefix; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function get($key, $default = null) |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->cache->get($this->prefix . $key, $default); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function set($key, $value, $ttl = null) |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->cache->set($this->prefix . $key, $value, $ttl); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function delete($key) |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->cache->delete($this->prefix . $key); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function clear() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->cache->clear(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getMultiple($keys, $default = null) |
|
55
|
|
|
{ |
|
56
|
|
|
$prefixedKeys = []; |
|
57
|
|
|
foreach ($keys as $key) { |
|
58
|
|
|
$prefixedKeys[] = $this->prefix . $key; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this->cache->getMultiple($prefixedKeys, $default); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function setMultiple($values, $ttl = null) |
|
65
|
|
|
{ |
|
66
|
|
|
$prefixedValues = []; |
|
67
|
|
|
foreach ($values as $key => $value) { |
|
68
|
|
|
$prefixedValues[$this->prefix . $key] = $value; |
|
69
|
|
|
} |
|
70
|
|
|
return $this->cache->setMultiple($prefixedValues, $ttl); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function deleteMultiple($keys) |
|
74
|
|
|
{ |
|
75
|
|
|
$prefixedKeys = []; |
|
76
|
|
|
foreach ($keys as $key) { |
|
77
|
|
|
$prefixedKeys[] = $this->prefix . $key; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->cache->deleteMultiple($prefixedKeys); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function has($key) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->cache->has($this->prefix . $key); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: