Passed
Pull Request — master (#206)
by Wilmer
13:02
created

SchemaCache   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
c 2
b 0
f 0
dl 0
loc 123
ccs 24
cts 27
cp 0.8889
rs 10
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A invalidate() 0 3 1
A isEnabled() 0 3 1
A setExclude() 0 3 1
A getDuration() 0 3 1
A remove() 0 3 1
A setDuration() 0 3 1
A get() 0 6 1
A isExcluded() 0 3 1
A set() 0 7 1
A setEnable() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Cache;
6
7
use Psr\SimpleCache\CacheInterface as PsrCacheInterface;
8
use Yiisoft\Cache\CacheInterface;
9
use Yiisoft\Cache\Dependency\Dependency;
10
use Yiisoft\Cache\Dependency\TagDependency;
11
12
/**
13
 * The cache application component that is used to cache the table metadata.
14
 */
15
final class SchemaCache
16
{
17
    private CacheInterface $cache;
18
    private PsrCacheInterface $psrCache;
19
    private bool $enabled = true;
20
    private int $duration = 3600;
21
    private array $exclude = [];
22
23 2988
    public function __construct(CacheInterface $cache, PsrCacheInterface $psrCache)
24
    {
25 2988
        $this->cache = $cache;
26 2988
        $this->psrCache = $psrCache;
27 2988
    }
28
29
    /**
30
     * Remove a value with the specified key from cache.
31
     *
32
     * @param mixed $key a key identifying the value to be deleted from cache.
33
     */
34
    public function remove($key): void
35
    {
36 99
        $this->cache->remove($key);
37
    }
38 99
39
    public function get($key, int $default = null)
40
    {
41
        return $this->cache->getOrSet(
42
            $key,
43
            static fn () => null,
44
            $default,
45
        );
46
    }
47
48
    /**
49
     * Return number of seconds that table metadata can remain valid in cache.
50
     *
51
     * @return int
52 1613
     */
53
    public function getDuration(): int
54 1613
    {
55
        return $this->duration;
56
    }
57 1608
58
    /**
59 1608
     * Return true if the table is excluded from cache the table metadata.
60
     *
61
     * @param string $value
62
     *
63
     * @return bool
64
     */
65
    public function isExcluded(string $value): bool
66
    {
67 1535
        return in_array($value, $this->exclude, true);
68
    }
69 1535
70
    /**
71
     * Invalidates all of the cached values that are associated with any of the specified {@see tags}.
72
     *
73
     * @param string $cacheTag
74
     */
75
    public function invalidate(string $cacheTag): void
76
    {
77 1595
        TagDependency::invalidate($this->psrCache, $cacheTag);
78
    }
79 1595
80
    /**
81
     * Return true if SchemaCache is active.
82
     *
83
     * @return bool
84
     */
85
    public function isEnabled(): bool
86
    {
87 96
        return $this->enabled;
88
    }
89 96
90 96
    public function set($key, $value, $ttl = null, Dependency $dependency = null): void
91
    {
92
        $this->cache->getOrSet(
93
            $key,
94
            static fn () => $value,
95
            $ttl,
96
            $dependency
97 1613
        );
98
    }
99 1613
100
    /**
101
     * Whether to enable schema caching. Note that in order to enable truly schema caching, a valid cache component as
102 1540
     * specified must be enabled and {@see setEnable()} must be set true.
103
     *
104 1540
     * @param bool $value
105
     *
106
     * {@see setduration()}
107
     * {@see setExclude()}
108
     */
109
    public function setEnable(bool $value): void
110
    {
111
        $this->enabled = $value;
112
    }
113
114
    /**
115
     * Number of seconds that table metadata can remain valid in cache. Use 0 to indicate that the cached data will
116 39
     * never expire.
117
     *
118 39
     * @param int $value
119 39
     *
120
     * {@see setEnable()}
121
     */
122
    public function setDuration(int $value): void
123
    {
124
        $this->duration = $value;
125
    }
126
127
    /**
128
     * List of tables whose metadata should NOT be cached. Defaults to empty array. The table names may contain schema
129
     * prefix, if any. Do not quote the table names.
130
     *
131
     * @param array $value
132
     *
133
     * {@see setEnable()}
134
     */
135
    public function setExclude(array $value): void
136
    {
137
        $this->exclude = $value;
138
    }
139
}
140