Passed
Push — master ( c5569e...25d5e8 )
by Rustam
11:00
created

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