|
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
|
|
|
use Yiisoft\Cache\Dependency\TagDependency; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The cache application component that is used to cache the table metadata. |
|
13
|
|
|
*/ |
|
14
|
|
|
final class SchemaCache |
|
15
|
|
|
{ |
|
16
|
|
|
private CacheInterface $cache; |
|
17
|
|
|
private bool $enabled = true; |
|
18
|
|
|
private int $duration = 3600; |
|
19
|
|
|
private array $exclude = []; |
|
20
|
|
|
|
|
21
|
2989 |
|
public function __construct(CacheInterface $cache) |
|
22
|
|
|
{ |
|
23
|
2989 |
|
$this->cache = $cache; |
|
24
|
2989 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Remove a value with the specified key from cache. |
|
28
|
|
|
* |
|
29
|
|
|
* @param mixed $key a key identifying the value to be deleted from cache. |
|
30
|
|
|
*/ |
|
31
|
100 |
|
public function remove($key): void |
|
32
|
|
|
{ |
|
33
|
100 |
|
$this->cache->remove($key); |
|
34
|
100 |
|
} |
|
35
|
|
|
|
|
36
|
1609 |
|
public function getOrSet($key, $value = null, $ttl = null, Dependency $dependency = null) |
|
37
|
|
|
{ |
|
38
|
1609 |
|
return $this->cache->getOrSet( |
|
39
|
1609 |
|
$key, |
|
40
|
1609 |
|
static fn () => $value, |
|
41
|
|
|
$ttl, |
|
42
|
|
|
$dependency, |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function set($key, $value, $ttl = null, Dependency $dependency = null) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->remove($key); |
|
49
|
|
|
$this->getOrSet($key, $value, $ttl, $dependency); |
|
50
|
1536 |
|
} |
|
51
|
|
|
|
|
52
|
1536 |
|
/** |
|
53
|
|
|
* Return number of seconds that table metadata can remain valid in cache. |
|
54
|
|
|
* |
|
55
|
|
|
* @return int |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getDuration(): int |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->duration; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1596 |
|
/** |
|
63
|
|
|
* Return true if the table is excluded from cache the table metadata. |
|
64
|
1596 |
|
* |
|
65
|
|
|
* @param string $value |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
public function isExcluded(string $value): bool |
|
70
|
|
|
{ |
|
71
|
|
|
return in_array($value, $this->exclude, true); |
|
72
|
96 |
|
} |
|
73
|
|
|
|
|
74
|
96 |
|
/** |
|
75
|
96 |
|
* Invalidates all of the cached values that are associated with any of the specified {@see tags}. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $cacheTag |
|
78
|
|
|
*/ |
|
79
|
|
|
public function invalidate(string $cacheTag): void |
|
80
|
|
|
{ |
|
81
|
|
|
TagDependency::invalidate($this->cache, $cacheTag); |
|
82
|
1614 |
|
} |
|
83
|
|
|
|
|
84
|
1614 |
|
/** |
|
85
|
|
|
* Return true if SchemaCache is active. |
|
86
|
|
|
* |
|
87
|
1541 |
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
1541 |
|
public function isEnabled(): bool |
|
90
|
1541 |
|
{ |
|
91
|
1541 |
|
return $this->enabled; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
1541 |
|
* Whether to enable schema caching. Note that in order to enable truly schema caching, a valid cache component as |
|
96
|
|
|
* specified must be enabled and {@see setEnable()} must be set true. |
|
97
|
|
|
* |
|
98
|
|
|
* @param bool $value |
|
99
|
|
|
* |
|
100
|
|
|
* {@see setduration()} |
|
101
|
|
|
* {@see setExclude()} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setEnable(bool $value): void |
|
104
|
|
|
{ |
|
105
|
|
|
$this->enabled = $value; |
|
106
|
39 |
|
} |
|
107
|
|
|
|
|
108
|
39 |
|
/** |
|
109
|
39 |
|
* Number of seconds that table metadata can remain valid in cache. Use 0 to indicate that the cached data will |
|
110
|
|
|
* never expire. |
|
111
|
|
|
* |
|
112
|
|
|
* @param int $value |
|
113
|
|
|
* |
|
114
|
|
|
* {@see setEnable()} |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setDuration(int $value): void |
|
117
|
|
|
{ |
|
118
|
|
|
$this->duration = $value; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* List of tables whose metadata should NOT be cached. Defaults to empty array. The table names may contain schema |
|
123
|
|
|
* prefix, if any. Do not quote the table names. |
|
124
|
|
|
* |
|
125
|
|
|
* @param array $value |
|
126
|
|
|
* |
|
127
|
|
|
* {@see setEnable()} |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setExclude(array $value): void |
|
130
|
|
|
{ |
|
131
|
|
|
$this->exclude = $value; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|