Passed
Push — master ( 6a1dba...c2b4c4 )
by Alexander
02:09
created

PrefixedCache::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Cache;
6
7
use Psr\SimpleCache\CacheInterface as PsrSimpleCacheInterface;
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 PsrSimpleCacheInterface
20
{
21
    private PsrSimpleCacheInterface $cache;
22
    private string $prefix;
23
24
    /**
25
     * @param PsrSimpleCacheInterface $cache PSR-16 cache to add prefix to.
26
     * @param string $prefix Prefix to use for all cache keys.
27
     */
28 4
    public function __construct(PsrSimpleCacheInterface $cache, string $prefix)
29
    {
30 4
        $this->cache = $cache;
31 4
        $this->prefix = $prefix;
32 4
    }
33
34 2
    public function get($key, $default = null)
35
    {
36 2
        return $this->cache->get($this->prefix . $key, $default);
37
    }
38
39 3
    public function set($key, $value, $ttl = null)
40
    {
41 3
        return $this->cache->set($this->prefix . $key, $value, $ttl);
42
    }
43
44 1
    public function delete($key)
45
    {
46 1
        return $this->cache->delete($this->prefix . $key);
47
    }
48
49 1
    public function clear()
50
    {
51 1
        return $this->cache->clear();
52
    }
53
54 1
    public function getMultiple($keys, $default = null)
55
    {
56 1
        $prefixedKeys = [];
57
58 1
        foreach ($keys as $key) {
59 1
            $prefixedKeys[] = $this->prefix . $key;
60
        }
61
62 1
        return $this->cache->getMultiple($prefixedKeys, $default);
63
    }
64
65 1
    public function setMultiple($values, $ttl = null)
66
    {
67 1
        $prefixedValues = [];
68
69 1
        foreach ($values as $key => $value) {
70 1
            $prefixedValues[$this->prefix . $key] = $value;
71
        }
72
73 1
        return $this->cache->setMultiple($prefixedValues, $ttl);
74
    }
75
76 1
    public function deleteMultiple($keys)
77
    {
78 1
        $prefixedKeys = [];
79
80 1
        foreach ($keys as $key) {
81 1
            $prefixedKeys[] = $this->prefix . $key;
82
        }
83
84 1
        return $this->cache->deleteMultiple($prefixedKeys);
85
    }
86
87 1
    public function has($key)
88
    {
89 1
        return $this->cache->has($this->prefix . $key);
90
    }
91
}
92