|
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 ConnectionCache |
|
16
|
|
|
{ |
|
17
|
|
|
private CacheInterface $schemaCache; |
|
18
|
|
|
private bool $enableSchemaCache = true; |
|
19
|
|
|
private int $schemaCacheDuration = 3600; |
|
20
|
|
|
private array $schemaCacheExclude = []; |
|
21
|
|
|
private bool $enableQueryCache = true; |
|
22
|
|
|
public array $queryCacheInfo = []; |
|
23
|
|
|
private int $queryCacheDuration = 3600; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(CacheInterface $schemaCache, CacheKeyNormalizer $cacheKeyNormalizer) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->schemaCache = $schemaCache; |
|
28
|
|
|
$this->cacheKeyNormalizer = $cacheKeyNormalizer; |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function normalize($key): string |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->cacheKeyNormalizer->normalize($key); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function getQueryCacheDuration(): ?int |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->queryCacheDuration; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getQueryCacheInfo(): array |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->queryCacheInfo; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getSchemaCache(): CacheInterface |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->schemaCache; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getSchemaCacheDuration(): int |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->schemaCacheDuration; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getSchemaCacheExclude(): array |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->schemaCacheExclude; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function isQueryCacheEnabled(): bool |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->enableQueryCache; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function isSchemaCacheEnabled(): bool |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->enableSchemaCache; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns the current query cache information. |
|
73
|
|
|
* |
|
74
|
|
|
* This method is used internally by {@see Command}. |
|
75
|
|
|
* |
|
76
|
|
|
* @param int|null $duration the preferred caching duration. If null, it will be ignored. |
|
77
|
|
|
* @param Dependency|null $dependency the preferred caching dependency. If null, it will be |
|
78
|
|
|
* ignored. |
|
79
|
|
|
* |
|
80
|
|
|
* @return array|null the current query cache information, or null if query cache is not enabled. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function queryCacheInfo(?int $duration, Dependency $dependency = null): ?array |
|
83
|
|
|
{ |
|
84
|
|
|
$result = null; |
|
85
|
|
|
|
|
86
|
|
|
if ($this->enableQueryCache) { |
|
87
|
|
|
$info = end($this->queryCacheInfo); |
|
88
|
|
|
|
|
89
|
|
|
if (is_array($info)) { |
|
90
|
|
|
if ($duration === null) { |
|
91
|
|
|
$duration = $info[0]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($dependency === null) { |
|
95
|
|
|
$dependency = $info[1]; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ($duration === 0 || $duration > 0) { |
|
100
|
|
|
if ($this->schemaCache instanceof CacheInterface) { |
|
|
|
|
|
|
101
|
|
|
$result = [$this->schemaCache, $duration, $dependency]; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $result; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function queryCacheInfoArrayPop(): void |
|
110
|
|
|
{ |
|
111
|
|
|
array_pop($this->queryCacheInfo); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Whether to enable query caching. Note that in order to enable query caching, a valid cache component as specified |
|
116
|
|
|
* by {@see setQueryCache()} must be enabled and {@see enableQueryCache} must be set true. Also, only the results of |
|
117
|
|
|
* the queries enclosed within {@see cache()} will be cached. |
|
118
|
|
|
* |
|
119
|
|
|
* @param bool $value |
|
120
|
|
|
* |
|
121
|
|
|
* {@see setQueryCache()} |
|
122
|
|
|
* {@see cache()} |
|
123
|
|
|
* {@see noCache()} |
|
124
|
|
|
*/ |
|
125
|
|
|
public function setEnableQueryCache(bool $value): void |
|
126
|
|
|
{ |
|
127
|
|
|
$this->enableQueryCache = $value; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Whether to enable schema caching. Note that in order to enable truly schema caching, a valid cache component as |
|
132
|
|
|
* specified by {@see setSchemaCache()} must be enabled and {@see setEnableSchemaCache()} must be set true. |
|
133
|
|
|
* |
|
134
|
|
|
* @param bool $value |
|
135
|
|
|
* |
|
136
|
|
|
* {@see setSchemaCacheDuration()} |
|
137
|
|
|
* {@see setSchemaCacheExclude()} |
|
138
|
|
|
* {@see setSchemaCache()} |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setEnableSchemaCache(bool $value): void |
|
141
|
|
|
{ |
|
142
|
|
|
$this->enableSchemaCache = $value; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function setQueryCacheInfo($value): void |
|
146
|
|
|
{ |
|
147
|
|
|
$this->queryCacheInfo[] = $value; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* The default number of seconds that query results can remain valid in cache. Defaults to 3600, meaning 3600 |
|
152
|
|
|
* seconds, or one hour. Use 0 to indicate that the cached data will never expire. The value of this property will |
|
153
|
|
|
* be used when {@see cache()} is called without a cache duration. |
|
154
|
|
|
* |
|
155
|
|
|
* @param int $value |
|
156
|
|
|
* |
|
157
|
|
|
* {@see setEnableQueryCache()} |
|
158
|
|
|
* {@see cache()} |
|
159
|
|
|
*/ |
|
160
|
|
|
public function setQueryCacheDuration(int $value): void |
|
161
|
|
|
{ |
|
162
|
|
|
$this->queryCacheDuration = $value; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Number of seconds that table metadata can remain valid in cache. Use 0 to indicate that the cached data will |
|
167
|
|
|
* never expire. |
|
168
|
|
|
* |
|
169
|
|
|
* @param int $value |
|
170
|
|
|
* |
|
171
|
|
|
* {@see setEnableSchemaCache()} |
|
172
|
|
|
*/ |
|
173
|
|
|
public function setSchemaCacheDuration(int $value): void |
|
174
|
|
|
{ |
|
175
|
|
|
$this->schemaCacheDuration = $value; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* List of tables whose metadata should NOT be cached. Defaults to empty array. The table names may contain schema |
|
180
|
|
|
* prefix, if any. Do not quote the table names. |
|
181
|
|
|
* |
|
182
|
|
|
* @param array $value |
|
183
|
|
|
* |
|
184
|
|
|
* {@see setEnableSchemaCache()} |
|
185
|
|
|
*/ |
|
186
|
|
|
public function setSchemaCacheExclude(array $value): void |
|
187
|
|
|
{ |
|
188
|
|
|
$this->schemaCacheExclude = $value; |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|