Passed
Pull Request — master (#215)
by Wilmer
13:47
created

SchemaCache::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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