Passed
Pull Request — master (#205)
by Wilmer
13:10
created

SchemaCache::isEnabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
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\CacheKeyNormalizer;
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 2988
    private bool $enabled = true;
19
    private int $duration = 3600;
20 2988
    private array $exclude = [];
21 2988
    private CacheKeyNormalizer $keyNormalizer;
22 2988
23
    public function __construct(CacheInterface $cache, CacheKeyNormalizer $keyNormalizer)
24 18
    {
25
        $this->cache = $cache;
26 18
        $this->keyNormalizer = $keyNormalizer;
27
    }
28
29 1613
    /**
30
     * Deletes a value with the specified key from cache.
31 1613
     *
32
     * @param mixed $key a key identifying the value to be deleted from cache.
33
     *
34 1535
     * @return bool if no error happens during deletion.
35
     */
36 1535
    public function delete($key): bool
37
    {
38
        return $this->cache->delete($key);
39 1595
    }
40
41 1595
    /**
42
     * Normalizes cache key from a given key.
43
     *
44 1613
     * If the given key is a string containing alphanumeric characters only and no more than 32 characters,
45
     * then the key will be returned back as it is, integers will be converted to strings. Otherwise,
46 1613
     * a normalized key is generated by serializing the given key and applying MD5 hashing.
47
     *
48
     * @param mixed $key The key to be normalized.
49
     *
50
     * @return string The normalized cache key.
51
     */
52
    public function normalize($key): string
53
    {
54
        return $this->keyNormalizer->normalize($key);
55
    }
56
57
    public function get($key, $default = null)
58 39
    {
59
        return $this->cache->get($key, $default);
60 39
    }
61 39
62
    /**
63
     * Return number of seconds that table metadata can remain valid in cache.
64
     *
65
     * @return int
66
     */
67
    public function getDuration(): int
68
    {
69
        return $this->duration;
70
    }
71
72
    /**
73
     * Return true if the table is excluded from cache the table metadata.
74
     *
75
     * @return bool
76
     */
77
    public function isExcluded(string $value): bool
78
    {
79
        return in_array($value, $this->exclude, true);
80
    }
81
82
    /**
83
     * Invalidates all of the cached values that are associated with any of the specified {@see tags}.
84
     *
85
     * @param string $tags
86
     */
87
    public function invalidate(string $cacheTag): void
88
    {
89
        TagDependency::invalidate($this->cache, $cacheTag);
90
    }
91
92
    /**
93
     * Return true if SchemaCache is active.
94
     *
95
     * @return bool
96
     */
97
    public function isEnabled(): bool
98
    {
99
        return $this->enabled;
100
    }
101
102
    public function set($key, $value, $ttl = null, Dependency $dependency = null): bool
103
    {
104
        return $this->cache->set($key, $value, $ttl, $dependency);
105
    }
106
107
    /**
108
     * Whether to enable schema caching. Note that in order to enable truly schema caching, a valid cache component as
109
     * specified must be enabled and {@see setEnable()} must be set true.
110
     *
111
     * @param bool $value
112
     *
113
     * {@see setduration()}
114
     * {@see setExclude()}
115
     */
116
    public function setEnable(bool $value): void
117
    {
118
        $this->enabled = $value;
119
    }
120
121
    /**
122
     * Number of seconds that table metadata can remain valid in cache. Use 0 to indicate that the cached data will
123
     * never expire.
124
     *
125
     * @param int $value
126
     *
127
     * {@see setEnable()}
128
     */
129
    public function setDuration(int $value): void
130
    {
131
        $this->duration = $value;
132
    }
133
134
    /**
135
     * List of tables whose metadata should NOT be cached. Defaults to empty array. The table names may contain schema
136
     * prefix, if any. Do not quote the table names.
137
     *
138
     * @param array $value
139
     *
140
     * {@see setEnable()}
141
     */
142
    public function setExclude(array $value): void
143
    {
144
        $this->exclude = $value;
145
    }
146
}
147