Passed
Pull Request — master (#212)
by Sergei
15:05
created

SchemaCache::getOrSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 7
ccs 4
cts 4
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 getOrSet($key, $value = null, $ttl = null, Dependency $dependency = null)
37
    {
38 1609
        return $this->cache->getOrSet(
39 1609
            $key,
40 1609
            static fn () => $value,
41
            $ttl,
42
            $dependency,
43
        );
44
    }
45
46
    public function set($key, $value, $ttl = null, Dependency $dependency = null)
47
    {
48
        $this->cache->remove($key);
49
        $this->cache->getOrSet(
50 1536
            $key,
51
            static fn () => $value,
52 1536
            $ttl,
53
            $dependency,
54
        );
55
    }
56
57
    /**
58
     * Return number of seconds that table metadata can remain valid in cache.
59
     *
60
     * @return int
61
     */
62 1596
    public function getDuration(): int
63
    {
64 1596
        return $this->duration;
65
    }
66
67
    /**
68
     * Return true if the table is excluded from cache the table metadata.
69
     *
70
     * @param string $value
71
     *
72 96
     * @return bool
73
     */
74 96
    public function isExcluded(string $value): bool
75 96
    {
76
        return in_array($value, $this->exclude, true);
77
    }
78
79
    /**
80
     * Invalidates all of the cached values that are associated with any of the specified {@see tags}.
81
     *
82 1614
     * @param string $cacheTag
83
     */
84 1614
    public function invalidate(string $cacheTag): void
85
    {
86
        TagDependency::invalidate($this->cache, $cacheTag);
87 1541
    }
88
89 1541
    /**
90 1541
     * Return true if SchemaCache is active.
91 1541
     *
92
     * @return bool
93
     */
94
    public function isEnabled(): bool
95 1541
    {
96
        return $this->enabled;
97
    }
98
99
    /**
100
     * Whether to enable schema caching. Note that in order to enable truly schema caching, a valid cache component as
101
     * specified must be enabled and {@see setEnable()} must be set true.
102
     *
103
     * @param bool $value
104
     *
105
     * {@see setduration()}
106 39
     * {@see setExclude()}
107
     */
108 39
    public function setEnable(bool $value): void
109 39
    {
110
        $this->enabled = $value;
111
    }
112
113
    /**
114
     * Number of seconds that table metadata can remain valid in cache. Use 0 to indicate that the cached data will
115
     * never expire.
116
     *
117
     * @param int $value
118
     *
119
     * {@see setEnable()}
120
     */
121
    public function setDuration(int $value): void
122
    {
123
        $this->duration = $value;
124
    }
125
126
    /**
127
     * List of tables whose metadata should NOT be cached. Defaults to empty array. The table names may contain schema
128
     * prefix, if any. Do not quote the table names.
129
     *
130
     * @param array $value
131
     *
132
     * {@see setEnable()}
133
     */
134
    public function setExclude(array $value): void
135
    {
136
        $this->exclude = $value;
137
    }
138
}
139