Passed
Pull Request — master (#197)
by Wilmer
11:50
created

QueryCache   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 103
rs 10
c 1
b 0
f 0
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A normalize() 0 3 1
A setEnable() 0 3 1
A getDuration() 0 3 1
A setInfo() 0 3 1
B info() 0 25 8
A isEnabled() 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 Psr\SimpleCache\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
final class QueryCache
16
{
17
    private CacheInterface $cache;
18
    private bool $enabled = true;
19
    public array $info = [];
20
    private int $duration = 3600;
21
    private CacheKeyNormalizer $keyNormalizer;
22
23
    public function __construct(CacheInterface $cache, CacheKeyNormalizer $keyNormalizer)
24
    {
25
        $this->cache = $cache;
26
        $this->keyNormalizer = $keyNormalizer;
27
    }
28
29
    public function normalize($key): string
30
    {
31
        return $this->keyNormalizer->normalize($key);
32
    }
33
34
    public function getDuration(): ?int
35
    {
36
        return $this->duration;
37
    }
38
39
    public function isEnabled(): bool
40
    {
41
        return $this->enabled;
42
    }
43
44
    /**
45
     * Returns the current query cache information.
46
     *
47
     * This method is used internally by {@see Command}.
48
     *
49
     * @param int|null $duration the preferred caching duration. If null, it will be ignored.
50
     * @param Dependency|null $dependency the preferred caching dependency. If null, it will be ignored.
51
     *
52
     * @return array|null the current query cache information, or null if query cache is not enabled.
53
     */
54
    public function info(?int $duration, Dependency $dependency = null): ?array
55
    {
56
        $result = null;
57
58
        if ($this->enabled) {
59
            $info = end($this->info);
60
61
            if (is_array($info)) {
62
                if ($duration === null) {
63
                    $duration = $info[0];
64
                }
65
66
                if ($dependency === null) {
67
                    $dependency = $info[1];
68
                }
69
            }
70
71
            if ($duration === 0 || $duration > 0) {
72
                if ($this->cache instanceof CacheInterface) {
0 ignored issues
show
introduced by
$this->cache is always a sub-type of Psr\SimpleCache\CacheInterface.
Loading history...
73
                    $result = [$this->cache, $duration, $dependency];
74
                }
75
            }
76
        }
77
78
        return $result;
79
    }
80
81
    public function removeLastInfo(): void
82
    {
83
        array_pop($this->info);
84
    }
85
86
    /**
87
     * Whether to enable query caching. Note that in order to enable query caching, a valid cache component as specified
88
     * must be enabled and {@see enabled} must be set true. Also, only the results of the queries enclosed within
89
     * {@see cache()} will be cached.
90
     *
91
     * @param bool $value
92
     *
93
     * {@see cache()}
94
     * {@see noCache()}
95
     */
96
    public function setEnable(bool $value): void
97
    {
98
        $this->enabled = $value;
99
    }
100
101
    public function setInfo($value): void
102
    {
103
        $this->info[] = $value;
104
    }
105
106
    /**
107
     * The default number of seconds that query results can remain valid in cache. Defaults to 3600, meaning 3600
108
     * seconds, or one hour. Use 0 to indicate that the cached data will never expire. The value of this property will
109
     * be used when {@see cache()} is called without a cache duration.
110
     *
111
     * @param int $value
112
     *
113
     * {@see cache()}
114
     */
115
    public function setDuration(int $value): void
116
    {
117
        $this->duration = $value;
118
    }
119
}
120