Passed
Push — master ( 14175d...40c9a5 )
by Alexander
09:38
created

QueryCache   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 87.88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 128
ccs 29
cts 33
cp 0.8788
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setEnable() 0 3 1
A getDuration() 0 3 1
A setInfo() 0 3 1
B info() 0 23 7
A isEnabled() 0 3 1
A normalize() 0 3 1
A removeLastInfo() 0 3 1
A setDuration() 0 3 1
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
11
use function array_pop;
12
use function end;
13
use function is_array;
14
15
/**
16
 * The cache application component that is used for query caching.
17
 */
18
final class QueryCache
19
{
20
    private CacheInterface $cache;
21
    private bool $enabled = true;
22
    public array $info = [];
23
    private int $duration = 3600;
24
    private CacheKeyNormalizer $keyNormalizer;
25
26 2988
    public function __construct(CacheInterface $cache, CacheKeyNormalizer $keyNormalizer)
27
    {
28 2988
        $this->cache = $cache;
29 2988
        $this->keyNormalizer = $keyNormalizer;
30 2988
    }
31
32
    /**
33
     * Normalizes cache key from a given key.
34
     *
35
     * If the given key is a string containing alphanumeric characters only and no more than 32 characters,
36
     * then the key will be returned back as it is, integers will be converted to strings. Otherwise,
37
     * a normalized key is generated by serializing the given key and applying MD5 hashing.
38
     *
39
     * @param mixed $key The key to be normalized.
40
     *
41
     * @return string The normalized cache key.
42
     */
43 10
    public function normalize($key): string
44
    {
45 10
        return $this->keyNormalizer->normalize($key);
46
    }
47
48
    /**
49
     * Return number of seconds that query results can remain valid in cache.
50
     *
51
     * @return int
52
     */
53 10
    public function getDuration(): ?int
54
    {
55 10
        return $this->duration;
56
    }
57
58
    /**
59
     * Return true if QueryCache is active.
60
     *
61
     * @return bool
62
     */
63
    public function isEnabled(): bool
64
    {
65
        return $this->enabled;
66
    }
67
68
    /**
69
     * Returns the current query cache information.
70
     *
71
     * This method is used internally by {@see Command}.
72
     *
73
     * @param int|null $duration the preferred caching duration. If null, it will be ignored.
74
     * @param Dependency|null $dependency the preferred caching dependency. If null, it will be ignored.
75
     *
76
     * @return array|null the current query cache information, or null if query cache is not enabled.
77
     */
78 1664
    public function info(?int $duration, Dependency $dependency = null): ?array
79
    {
80 1664
        $result = null;
81
82 1664
        if ($this->enabled) {
83 1664
            $info = end($this->info);
84
85 1664
            if (is_array($info)) {
86 10
                if ($duration === null) {
87 10
                    $duration = $info[0];
88
                }
89
90 10
                if ($dependency === null) {
91 10
                    $dependency = $info[1];
92
                }
93
            }
94
95 1664
            if ($duration === 0 || $duration > 0) {
96 10
                $result = [$this->cache, $duration, $dependency];
97
            }
98
        }
99
100 1664
        return $result;
101
    }
102
103
    /**
104
     * Extract the last element from the end of the QueryCache information.
105
     */
106 10
    public function removeLastInfo(): void
107
    {
108 10
        array_pop($this->info);
109 10
    }
110
111
    /**
112
     * Whether to enable query caching. Note that in order to enable query caching, a valid cache component as specified
113
     * must be enabled and {@see enabled} must be set true. Also, only the results of the queries enclosed within
114
     * {@see cache()} will be cached.
115
     *
116
     * @param bool $value
117
     *
118
     * {@see cache()}
119
     * {@see noCache()}
120
     */
121 10
    public function setEnable(bool $value): void
122
    {
123 10
        $this->enabled = $value;
124 10
    }
125
126
    /**
127
     * Add an element to the array that QueryCache information.
128
     */
129 10
    public function setInfo($value): void
130
    {
131 10
        $this->info[] = $value;
132 10
    }
133
134
    /**
135
     * The default number of seconds that query results can remain valid in cache. Defaults to 3600, meaning 3600
136
     * seconds, or one hour. Use 0 to indicate that the cached data will never expire. The value of this property will
137
     * be used when {@see cache()} is called without a cache duration.
138
     *
139
     * @param int $value
140
     *
141
     * {@see cache()}
142
     */
143
    public function setDuration(int $value): void
144
    {
145
        $this->duration = $value;
146
    }
147
}
148