Passed
Push — master ( 5f4e36...3f9ee1 )
by Alexander
02:07
created

PrefixedCache   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 22
c 1
b 0
f 0
dl 0
loc 67
ccs 0
cts 28
cp 0
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 3 1
A getMultiple() 0 8 2
A clear() 0 3 1
A __construct() 0 4 1
A setMultiple() 0 7 2
A deleteMultiple() 0 8 2
A has() 0 3 1
A get() 0 3 1
A delete() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Cache;
6
7
use Psr\SimpleCache\CacheInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Cache\CacheInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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