PrefixedCache::clear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
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 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Cache;
6
7
use DateInterval;
8
use Psr\SimpleCache\CacheInterface as PsrSimpleCacheInterface;
9
10
/**
11
 * PrefixedCache decorates any PSR-16 cache to add global prefix. It is added to every cache key so that it is unique
12
 * globally in the whole cache storage. It is recommended that you set a unique cache key prefix for each application
13
 * if the same cache storage is being used by different applications.
14
 *
15
 * ```php
16
 * $cache = new PrefixedCache(new ArrayCache(), 'my_app_');
17
 * $cache->set('answer', 42); // Will set 42 to my_app_answer key.
18
 * ```
19
 */
20
final class PrefixedCache implements PsrSimpleCacheInterface
21
{
22
    /**
23
     * @param PsrSimpleCacheInterface $cache PSR-16 cache to add prefix to.
24
     * @param string $prefix Prefix to use for all cache keys.
25
     */
26
    public function __construct(
27
        private PsrSimpleCacheInterface $cache,
28
        private string $prefix
29 4
    ) {
30
    }
31 4
32 4
    public function get(string $key, mixed $default = null): mixed
33
    {
34
        return $this->cache->get($this->prefix . $key, $default);
35 2
    }
36
37 2
    public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
38
    {
39
        return $this->cache->set($this->prefix . $key, $value, $ttl);
40 3
    }
41
42 3
    public function delete(string $key): bool
43
    {
44
        return $this->cache->delete($this->prefix . $key);
45 1
    }
46
47 1
    public function clear(): bool
48
    {
49
        return $this->cache->clear();
50 1
    }
51
52 1
    public function getMultiple(iterable $keys, mixed $default = null): iterable
53
    {
54
        $prefixedKeys = [];
55 1
56
        foreach ($keys as $key) {
57 1
            $prefixedKeys[] = $this->prefix . $key;
58
        }
59 1
60 1
        return $this->cache->getMultiple($prefixedKeys, $default);
61
    }
62
63 1
    public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
64
    {
65
        $prefixedValues = [];
66 1
67
        /**
68 1
         * @var string $key
69
         */
70
        foreach ($values as $key => $value) {
71
            $prefixedValues[$this->prefix . $key] = $value;
72
        }
73
74 1
        return $this->cache->setMultiple($prefixedValues, $ttl);
75
    }
76 1
77
    public function deleteMultiple(iterable $keys): bool
78
    {
79 1
        $prefixedKeys = [];
80
81
        foreach ($keys as $key) {
82 1
            $prefixedKeys[] = $this->prefix . $key;
83
        }
84 1
85
        return $this->cache->deleteMultiple($prefixedKeys);
86 1
    }
87 1
88
    public function has(string $key): bool
89
    {
90 1
        return $this->cache->has($this->prefix . $key);
91
    }
92
}
93