Skip to content

Commit

Permalink
Replace SchemaCache::get to SchemaCache::getOrSet for correct work wi…
Browse files Browse the repository at this point in the history
…th PSR caches (#212)
  • Loading branch information
vjik committed Jan 17, 2021
1 parent 25d5e8f commit 4949a3d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
23 changes: 10 additions & 13 deletions src/Cache/SchemaCache.php
Expand Up @@ -33,15 +33,22 @@ public function remove($key): void
$this->cache->remove($key);
}

public function get($key, int $default = null)
public function getOrSet($key, $value = null, $ttl = null, Dependency $dependency = null)
{
return $this->cache->getOrSet(
$key,
static fn () => null,
$default,
static fn () => $value,
$ttl,
$dependency,
);
}

public function set($key, $value, $ttl = null, Dependency $dependency = null)
{
$this->remove($key);
$this->getOrSet($key, $value, $ttl, $dependency);
}

/**
* Return number of seconds that table metadata can remain valid in cache.
*
Expand Down Expand Up @@ -84,16 +91,6 @@ public function isEnabled(): bool
return $this->enabled;
}

public function set($key, $value, $ttl = null, Dependency $dependency = null): void
{
$this->cache->getOrSet(
$key,
static fn () => $value,
$ttl,
$dependency
);
}

/**
* Whether to enable schema caching. Note that in order to enable truly schema caching, a valid cache component as
* specified must be enabled and {@see setEnable()} must be set true.
Expand Down
5 changes: 4 additions & 1 deletion src/Connection/Connection.php
Expand Up @@ -804,7 +804,10 @@ protected function openFromPoolSequentially(array $pool): ?self

$key = [__METHOD__, $db->getDsn()];

if ($this->schemaCache->isEnabled() && $this->schemaCache->get($key)) {
if (
$this->schemaCache->isEnabled() &&
$this->schemaCache->getOrSet($key, null, $this->serverRetryInterval)
) {
/** should not try this dead server now */
continue;
}
Expand Down
9 changes: 7 additions & 2 deletions src/Schema/Schema.php
Expand Up @@ -863,7 +863,7 @@ protected function getSchemaMetadata(string $schema, string $type, bool $refresh
*
* @param string $name table name.
* @param string $type metadata type.
* @param mixed $data metadata.
* @param mixed $data metadata.
*/
protected function setTableMetadata(string $name, string $type, $data): void
{
Expand Down Expand Up @@ -910,7 +910,12 @@ private function loadTableMetadataFromCache(string $rawName): void
return;
}

$metadata = $this->schemaCache->get($this->getCacheKey($rawName));
$metadata = $this->schemaCache->getOrSet(
$this->getCacheKey($rawName),
null,
$this->schemaCache->getDuration(),
new TagDependency(['tags' => $this->getCacheTag()]),
);

if (
!is_array($metadata) ||
Expand Down

0 comments on commit 4949a3d

Please sign in to comment.