Skip to content

Commit 40c9a56

Browse files
Clean up
1 parent 14175da commit 40c9a56

5 files changed

Lines changed: 125 additions & 61 deletions

File tree

src/Cache/QueryCache.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

55
namespace Yiisoft\Db\Cache;
66

7-
use Psr\SimpleCache\CacheInterface;
7+
use Yiisoft\Cache\CacheInterface;
88
use Yiisoft\Cache\CacheKeyNormalizer;
99
use Yiisoft\Cache\Dependency\Dependency;
1010

1111
use function array_pop;
1212
use function end;
1313
use function is_array;
1414

15+
/**
16+
* The cache application component that is used for query caching.
17+
*/
1518
final class QueryCache
1619
{
1720
private CacheInterface $cache;
@@ -26,16 +29,37 @@ public function __construct(CacheInterface $cache, CacheKeyNormalizer $keyNormal
2629
$this->keyNormalizer = $keyNormalizer;
2730
}
2831

32+
/**
33+
* Normalizes cache key from a given key.
34+
*
35+
* If the given key is a string containing alphanumeric characters only and no more than 32 characters,
36+
* then the key will be returned back as it is, integers will be converted to strings. Otherwise,
37+
* a normalized key is generated by serializing the given key and applying MD5 hashing.
38+
*
39+
* @param mixed $key The key to be normalized.
40+
*
41+
* @return string The normalized cache key.
42+
*/
2943
public function normalize($key): string
3044
{
3145
return $this->keyNormalizer->normalize($key);
3246
}
3347

48+
/**
49+
* Return number of seconds that query results can remain valid in cache.
50+
*
51+
* @return int
52+
*/
3453
public function getDuration(): ?int
3554
{
3655
return $this->duration;
3756
}
3857

58+
/**
59+
* Return true if QueryCache is active.
60+
*
61+
* @return bool
62+
*/
3963
public function isEnabled(): bool
4064
{
4165
return $this->enabled;
@@ -69,15 +93,16 @@ public function info(?int $duration, Dependency $dependency = null): ?array
6993
}
7094

7195
if ($duration === 0 || $duration > 0) {
72-
if ($this->cache instanceof CacheInterface) {
73-
$result = [$this->cache, $duration, $dependency];
74-
}
96+
$result = [$this->cache, $duration, $dependency];
7597
}
7698
}
7799

78100
return $result;
79101
}
80102

103+
/**
104+
* Extract the last element from the end of the QueryCache information.
105+
*/
81106
public function removeLastInfo(): void
82107
{
83108
array_pop($this->info);
@@ -98,6 +123,9 @@ public function setEnable(bool $value): void
98123
$this->enabled = $value;
99124
}
100125

126+
/**
127+
* Add an element to the array that QueryCache information.
128+
*/
101129
public function setInfo($value): void
102130
{
103131
$this->info[] = $value;

src/Cache/SchemaCache.php

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
namespace Yiisoft\Db\Cache;
66

7-
use Psr\SimpleCache\CacheInterface;
7+
use Yiisoft\Cache\CacheInterface;
88
use Yiisoft\Cache\CacheKeyNormalizer;
9+
use Yiisoft\Cache\Dependency\Dependency;
10+
use Yiisoft\Cache\Dependency\TagDependency;
911

12+
/**
13+
* The cache application component that is used to cache the table metadata.
14+
*/
1015
final class SchemaCache
1116
{
1217
private CacheInterface $cache;
@@ -21,31 +26,84 @@ public function __construct(CacheInterface $cache, CacheKeyNormalizer $keyNormal
2126
$this->keyNormalizer = $keyNormalizer;
2227
}
2328

29+
/**
30+
* Deletes a value with the specified key from cache.
31+
*
32+
* @param mixed $key a key identifying the value to be deleted from cache.
33+
*
34+
* @return bool if no error happens during deletion.
35+
*/
36+
public function delete($key): bool
37+
{
38+
return $this->cache->delete($key);
39+
}
40+
41+
/**
42+
* Normalizes cache key from a given key.
43+
*
44+
* If the given key is a string containing alphanumeric characters only and no more than 32 characters,
45+
* then the key will be returned back as it is, integers will be converted to strings. Otherwise,
46+
* a normalized key is generated by serializing the given key and applying MD5 hashing.
47+
*
48+
* @param mixed $key The key to be normalized.
49+
*
50+
* @return string The normalized cache key.
51+
*/
2452
public function normalize($key): string
2553
{
2654
return $this->keyNormalizer->normalize($key);
2755
}
2856

29-
public function getCache(): CacheInterface
57+
public function get($key, $default = null)
3058
{
31-
return $this->cache;
59+
return $this->cache->get($key, $default);
3260
}
3361

62+
/**
63+
* Return number of seconds that table metadata can remain valid in cache.
64+
*
65+
* @return int
66+
*/
3467
public function getDuration(): int
3568
{
3669
return $this->duration;
3770
}
3871

39-
public function isExclude(string $value): bool
72+
/**
73+
* Return true if the table is excluded from cache the table metadata.
74+
*
75+
* @return bool
76+
*/
77+
public function isExcluded(string $value): bool
4078
{
41-
return !in_array($value, $this->exclude, true);
79+
return in_array($value, $this->exclude, true);
4280
}
4381

82+
/**
83+
* Invalidates all of the cached values that are associated with any of the specified {@see tags}.
84+
*
85+
* @param string $tags
86+
*/
87+
public function invalidate(string $cacheTag): void
88+
{
89+
TagDependency::invalidate($this->cache, $cacheTag);
90+
}
91+
92+
/**
93+
* Return true if SchemaCache is active.
94+
*
95+
* @return bool
96+
*/
4497
public function isEnabled(): bool
4598
{
4699
return $this->enabled;
47100
}
48101

102+
public function set($key, $value, $ttl = null, Dependency $dependency = null): bool
103+
{
104+
return $this->cache->set($key, $value, $ttl, $dependency);
105+
}
106+
49107
/**
50108
* Whether to enable schema caching. Note that in order to enable truly schema caching, a valid cache component as
51109
* specified must be enabled and {@see setEnable()} must be set true.

src/Connection/Connection.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -796,15 +796,13 @@ protected function openFromPoolSequentially(array $pool): ?self
796796
return null;
797797
}
798798

799-
$cache = $this->schemaCache->getCache();
800-
801799
foreach ($pool as $config) {
802800
/* @var $db Connection */
803801
$db = DatabaseFactory::createClass($config);
804802

805803
$key = $this->schemaCache->normalize([__METHOD__, $db->getDsn()]);
806804

807-
if ($this->schemaCache->isEnabled() && $cache->get($key)) {
805+
if ($this->schemaCache->isEnabled() && $this->schemaCache->get($key)) {
808806
/** should not try this dead server now */
809807
continue;
810808
}
@@ -823,7 +821,7 @@ protected function openFromPoolSequentially(array $pool): ?self
823821

824822
if ($this->schemaCache->isEnabled()) {
825823
/** mark this server as dead and only retry it after the specified interval */
826-
$cache->set($key, 1, $this->serverRetryInterval);
824+
$this->schemaCache->set($key, 1, $this->serverRetryInterval);
827825
}
828826

829827
return null;

src/Schema/Schema.php

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use JsonException;
88
use PDO;
99
use PDOException;
10-
use Psr\SimpleCache\CacheInterface;
1110
use Throwable;
1211
use Yiisoft\Cache\Dependency\TagDependency;
1312
use Yiisoft\Db\Cache\SchemaCache;
@@ -303,11 +302,8 @@ public function getPdoType($data): int
303302
*/
304303
public function refresh(): void
305304
{
306-
/* @var $cache CacheInterface */
307-
$cache = $this->schemaCache->getCache();
308-
309305
if ($this->schemaCache->isEnabled()) {
310-
TagDependency::invalidate($cache, $this->getCacheTag());
306+
$this->schemaCache->invalidate($this->getCacheTag());
311307
}
312308

313309
$this->tableNames = [];
@@ -333,7 +329,7 @@ public function refreshTableSchema(string $name): void
333329
$this->tableNames = [];
334330

335331
if ($this->schemaCache->isEnabled()) {
336-
$this->schemaCache->getCache()->delete($this->getCacheKey($rawName));
332+
$this->schemaCache->delete($this->getCacheKey($rawName));
337333
}
338334
}
339335

@@ -768,9 +764,9 @@ public function getServerVersion(): string
768764
*
769765
* @throws JsonException
770766
*
771-
* @return mixed the cache key.
767+
* @return string the cache key.
772768
*/
773-
protected function getCacheKey(string $name)
769+
protected function getCacheKey(string $name): string
774770
{
775771
$key = [
776772
__CLASS__,
@@ -779,9 +775,7 @@ protected function getCacheKey(string $name)
779775
$this->getRawTableName($name),
780776
];
781777

782-
$jsonKey = json_encode($key, JSON_THROW_ON_ERROR);
783-
784-
return md5($jsonKey);
778+
return $this->schemaCache->normalize($key);
785779
}
786780

787781
/**
@@ -816,19 +810,15 @@ protected function getCacheTag(): string
816810
*/
817811
protected function getTableMetadata(string $name, string $type, bool $refresh = false)
818812
{
819-
if ($this->schemaCache->isEnabled() && $this->schemaCache->isExclude($name)) {
820-
$schemaCache = $this->schemaCache->getCache();
821-
}
822-
823813
$rawName = $this->getRawTableName($name);
824814

825815
if (!isset($this->tableMetadata[$rawName])) {
826-
$this->loadTableMetadataFromCache($schemaCache, $rawName);
816+
$this->loadTableMetadataFromCache($rawName);
827817
}
828818

829819
if ($refresh || !array_key_exists($type, $this->tableMetadata[$rawName])) {
830820
$this->tableMetadata[$rawName][$type] = $this->{'loadTable' . ucfirst($type)}($rawName);
831-
$this->saveTableMetadataToCache($schemaCache, $rawName);
821+
$this->saveTableMetadataToCache($rawName);
832822
}
833823

834824
return $this->tableMetadata[$rawName][$type];
@@ -910,55 +900,53 @@ protected function normalizePdoRowKeyCase(array $row, bool $multiple): array
910900
/**
911901
* Tries to load and populate table metadata from cache.
912902
*
913-
* @param CacheInterface|null $cache
914-
* @param string $name
903+
* @param string $rawName
915904
*
916905
* @throws JsonException
917906
*/
918-
private function loadTableMetadataFromCache(?CacheInterface $cache, string $name): void
907+
private function loadTableMetadataFromCache(string $rawName): void
919908
{
920-
if ($cache === null) {
921-
$this->tableMetadata[$name] = [];
909+
if ($this->schemaCache->isEnabled() === false || $this->schemaCache->isExcluded($rawName) === true) {
910+
$this->tableMetadata[$rawName] = [];
922911

923912
return;
924913
}
925914

926-
$metadata = $cache->get($this->getCacheKey($name));
915+
$metadata = $this->schemaCache->get($this->getCacheKey($rawName));
927916

928917
if (
929918
!is_array($metadata) ||
930919
!isset($metadata['cacheVersion']) ||
931920
$metadata['cacheVersion'] !== static::SCHEMA_CACHE_VERSION
932921
) {
933-
$this->tableMetadata[$name] = [];
922+
$this->tableMetadata[$rawName] = [];
934923

935924
return;
936925
}
937926

938927
unset($metadata['cacheVersion']);
939-
$this->tableMetadata[$name] = $metadata;
928+
$this->tableMetadata[$rawName] = $metadata;
940929
}
941930

942931
/**
943932
* Saves table metadata to cache.
944933
*
945-
* @param CacheInterface|null $cache
946-
* @param string $name
934+
* @param string $rawName
947935
*
948936
* @throws JsonException
949937
*/
950-
private function saveTableMetadataToCache(?CacheInterface $cache, string $name): void
938+
private function saveTableMetadataToCache(string $rawName): void
951939
{
952-
if ($cache === null) {
940+
if ($this->schemaCache->isEnabled() === false || $this->schemaCache->isExcluded($rawName) === true) {
953941
return;
954942
}
955943

956-
$metadata = $this->tableMetadata[$name];
944+
$metadata = $this->tableMetadata[$rawName];
957945

958946
$metadata['cacheVersion'] = static::SCHEMA_CACHE_VERSION;
959947

960-
$cache->set(
961-
$this->getCacheKey($name),
948+
$this->schemaCache->set(
949+
$this->getCacheKey($rawName),
962950
$metadata,
963951
$this->schemaCache->getDuration(),
964952
new TagDependency(['tags' => $this->getCacheTag()]),

0 commit comments

Comments
 (0)