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