|
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 $enableCache = true; |
|
19
|
|
|
public array $cacheInfo = []; |
|
20
|
|
|
private int $cacheDuration = 3600; |
|
21
|
|
|
private CacheKeyNormalizer $cacheKeyNormalizer; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(CacheInterface $cache, CacheKeyNormalizer $cacheKeyNormalizer) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->cache = $cache; |
|
26
|
|
|
$this->cacheKeyNormalizer = $cacheKeyNormalizer; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function normalize($key): string |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->cacheKeyNormalizer->normalize($key); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getCacheDuration(): ?int |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->cacheDuration; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getCacheInfo(): array |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->cacheInfo; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function isCacheEnabled(): bool |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->enableCache; |
|
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 |
|
56
|
|
|
* ignored. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array|null the current query cache information, or null if query cache is not enabled. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function cacheInfo(?int $duration, Dependency $dependency = null): ?array |
|
61
|
|
|
{ |
|
62
|
|
|
$result = null; |
|
63
|
|
|
|
|
64
|
|
|
if ($this->enableCache) { |
|
65
|
|
|
$info = end($this->cacheInfo); |
|
66
|
|
|
|
|
67
|
|
|
if (is_array($info)) { |
|
68
|
|
|
if ($duration === null) { |
|
69
|
|
|
$duration = $info[0]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($dependency === null) { |
|
73
|
|
|
$dependency = $info[1]; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($duration === 0 || $duration > 0) { |
|
78
|
|
|
if ($this->cache instanceof CacheInterface) { |
|
|
|
|
|
|
79
|
|
|
$result = [$this->cache, $duration, $dependency]; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function cacheInfoArrayPop(): void |
|
88
|
|
|
{ |
|
89
|
|
|
array_pop($this->cacheInfo); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Whether to enable query caching. Note that in order to enable query caching, a valid cache component as specified |
|
94
|
|
|
* by {@see setQueryCache()} must be enabled and {@see enableQueryCache} must be set true. Also, only the results of |
|
95
|
|
|
* the queries enclosed within {@see cache()} will be cached. |
|
96
|
|
|
* |
|
97
|
|
|
* @param bool $value |
|
98
|
|
|
* |
|
99
|
|
|
* {@see setQueryCache()} |
|
100
|
|
|
* {@see cache()} |
|
101
|
|
|
* {@see noCache()} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setEnableCache(bool $value): void |
|
104
|
|
|
{ |
|
105
|
|
|
$this->enableCache = $value; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function setCacheInfo($value): void |
|
109
|
|
|
{ |
|
110
|
|
|
$this->cacheInfo[] = $value; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* The default number of seconds that query results can remain valid in cache. Defaults to 3600, meaning 3600 |
|
115
|
|
|
* seconds, or one hour. Use 0 to indicate that the cached data will never expire. The value of this property will |
|
116
|
|
|
* be used when {@see cache()} is called without a cache duration. |
|
117
|
|
|
* |
|
118
|
|
|
* @param int $value |
|
119
|
|
|
* |
|
120
|
|
|
* {@see setEnableQueryCache()} |
|
121
|
|
|
* {@see cache()} |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setCacheDuration(int $value): void |
|
124
|
|
|
{ |
|
125
|
|
|
$this->cacheDuration = $value; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|