Passed
Pull Request — master (#197)
by Wilmer
16:15
created

SchemaCache::setEnableCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Cache;
6
7
use Psr\SimpleCache\CacheInterface;
8
use Yiisoft\Cache\CacheKeyNormalizer;
9
10
final class SchemaCache
11
{
12
    private CacheInterface $cache;
13
    private bool $enableCache = true;
14
    private int $cacheDuration = 3600;
15
    private array $cacheExclude = [];
16
    private CacheKeyNormalizer $cacheKeyNormalizer;
17
18
    public function __construct(CacheInterface $cache, CacheKeyNormalizer $cacheKeyNormalizer)
19
    {
20
        $this->cache = $cache;
21
        $this->cacheKeyNormalizer = $cacheKeyNormalizer;
22
    }
23
24
    public function normalize($key): string
25
    {
26
        return $this->cacheKeyNormalizer->normalize($key);
27
    }
28
29
    public function getCache(): CacheInterface
30
    {
31
        return $this->cache;
32
    }
33
34
    public function getCacheDuration(): int
35
    {
36
        return $this->cacheDuration;
37
    }
38
39
    public function getCacheExclude(): array
40
    {
41
        return $this->cacheExclude;
42
    }
43
44
    public function isCacheEnabled(): bool
45
    {
46
        return $this->enableCache;
47
    }
48
49
    /**
50
     * Whether to enable schema caching. Note that in order to enable truly schema caching, a valid cache component as
51
     * specified by {@see setSchemaCache()} must be enabled and {@see setEnableSchemaCache()} must be set true.
52
     *
53
     * @param bool $value
54
     *
55
     * {@see setSchemaCacheDuration()}
56
     * {@see setSchemaCacheExclude()}
57
     * {@see setSchemaCache()}
58
     */
59
    public function setEnableCache(bool $value): void
60
    {
61
        $this->enableCache = $value;
62
    }
63
64
    /**
65
     * Number of seconds that table metadata can remain valid in cache. Use 0 to indicate that the cached data will
66
     * never expire.
67
     *
68
     * @param int $value
69
     *
70
     * {@see setEnableSchemaCache()}
71
     */
72
    public function setCacheDuration(int $value): void
73
    {
74
        $this->cacheDuration = $value;
75
    }
76
77
    /**
78
     * List of tables whose metadata should NOT be cached. Defaults to empty array. The table names may contain schema
79
     * prefix, if any. Do not quote the table names.
80
     *
81
     * @param array $value
82
     *
83
     * {@see setEnableSchemaCache()}
84
     */
85
    public function setCacheExclude(array $value): void
86
    {
87
        $this->cacheExclude = $value;
88
    }
89
}
90