Skip to content

Commit

Permalink
Adjust to changes in yiisoft/cache
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Dec 18, 2020
1 parent 3cbab24 commit c5569e2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 71 deletions.
23 changes: 3 additions & 20 deletions src/Cache/QueryCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Yiisoft\Db\Cache;

use Yiisoft\Cache\CacheInterface;
use Yiisoft\Cache\CacheKeyNormalizer;
use Yiisoft\Cache\Dependency\Dependency;

use function array_pop;
Expand All @@ -21,28 +20,10 @@ final class QueryCache
private bool $enabled = true;
public array $info = [];
private int $duration = 3600;
private CacheKeyNormalizer $keyNormalizer;

public function __construct(CacheInterface $cache, CacheKeyNormalizer $keyNormalizer)
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
$this->keyNormalizer = $keyNormalizer;
}

/**
* Normalizes cache key from a given key.
*
* If the given key is a string containing alphanumeric characters only and no more than 32 characters,
* then the key will be returned back as it is, integers will be converted to strings. Otherwise,
* a normalized key is generated by serializing the given key and applying MD5 hashing.
*
* @param mixed $key The key to be normalized.
*
* @return string The normalized cache key.
*/
public function normalize($key): string
{
return $this->keyNormalizer->normalize($key);
}

/**
Expand Down Expand Up @@ -125,6 +106,8 @@ public function setEnable(bool $value): void

/**
* Add an element to the array that QueryCache information.
*
* @param $value
*/
public function setInfo($value): void
{
Expand Down
50 changes: 20 additions & 30 deletions src/Cache/SchemaCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Yiisoft\Db\Cache;

use Yiisoft\Cache\CacheInterface;
use Yiisoft\Cache\CacheKeyNormalizer;
use Yiisoft\Cache\Dependency\Dependency;
use Yiisoft\Cache\Dependency\TagDependency;

Expand All @@ -18,45 +17,29 @@ final class SchemaCache
private bool $enabled = true;
private int $duration = 3600;
private array $exclude = [];
private CacheKeyNormalizer $keyNormalizer;

public function __construct(CacheInterface $cache, CacheKeyNormalizer $keyNormalizer)
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
$this->keyNormalizer = $keyNormalizer;
}

/**
* Deletes a value with the specified key from cache.
* Remove a value with the specified key from cache.
*
* @param mixed $key a key identifying the value to be deleted from cache.
*
* @return bool if no error happens during deletion.
*/
public function delete($key): bool
{
return $this->cache->delete($key);
}

/**
* Normalizes cache key from a given key.
*
* If the given key is a string containing alphanumeric characters only and no more than 32 characters,
* then the key will be returned back as it is, integers will be converted to strings. Otherwise,
* a normalized key is generated by serializing the given key and applying MD5 hashing.
*
* @param mixed $key The key to be normalized.
*
* @return string The normalized cache key.
*/
public function normalize($key): string
public function remove($key): void
{
return $this->keyNormalizer->normalize($key);
$this->cache->remove($key);
}

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

/**
Expand All @@ -72,6 +55,8 @@ public function getDuration(): int
/**
* Return true if the table is excluded from cache the table metadata.
*
* @param string $value
*
* @return bool
*/
public function isExcluded(string $value): bool
Expand All @@ -82,7 +67,7 @@ public function isExcluded(string $value): bool
/**
* Invalidates all of the cached values that are associated with any of the specified {@see tags}.
*
* @param string $tags
* @param string $cacheTag
*/
public function invalidate(string $cacheTag): void
{
Expand All @@ -99,9 +84,14 @@ public function isEnabled(): bool
return $this->enabled;
}

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

/**
Expand Down
23 changes: 14 additions & 9 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -1329,9 +1329,10 @@ protected function queryInternal(string $method, $fetchMode = null)
$cache = $info[0];
$rawSql = $rawSql ?: $this->getRawSql();
$cacheKey = $this->getCacheKey($method, $fetchMode, $rawSql);


$result = $cache->get($cacheKey);
$result = $cache->getOrSet(
$cacheKey,
static fn () => null,
);

if (is_array($result) && isset($result[0])) {
if ($this->db->isLoggingEnabled()) {
Expand Down Expand Up @@ -1380,7 +1381,13 @@ protected function queryInternal(string $method, $fetchMode = null)
}

if (isset($cache, $cacheKey, $info)) {
$cache->set($cacheKey, [$result], $info[1], $info[2]);
$cache->getOrSet(
$cacheKey,
static fn (): array => [$result],
$info[1],
$info[2]
);

if ($this->db->isLoggingEnabled()) {
$this->logger->log(
LogLevel::DEBUG,
Expand All @@ -1404,20 +1411,18 @@ protected function queryInternal(string $method, $fetchMode = null)
*
* @throws JsonException
*
* @return string the cache key.
* @return array the cache key.
*/
protected function getCacheKey(string $method, ?int $fetchMode, string $rawSql): string
protected function getCacheKey(string $method, ?int $fetchMode, string $rawSql): array
{
$key = [
return [
__CLASS__,
$method,
$fetchMode,
$this->db->getDsn(),
$this->db->getUsername(),
$rawSql,
];

return $this->queryCache->normalize($key);
}

/**
Expand Down
16 changes: 10 additions & 6 deletions src/Connection/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Db\Connection;

use JsonException;
use PDO;
use PDOException;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -447,8 +448,6 @@ public function getLogger(): LoggerInterface
*
* If this method is called for the first time, it will try to open a master connection.
*
* @throws InvalidConfigException
*
* @return Connection the currently active master connection. `null` is returned if there is no master available.
*/
public function getMaster(): ?self
Expand Down Expand Up @@ -515,6 +514,9 @@ public function getProfiler(): Profiler
/**
* Returns a server version as a string comparable by {@see \version_compare()}.
*
* @throws Exception
* @throws InvalidConfigException
*
* @return string server version as a string.
*/
public function getServerVersion(): string
Expand All @@ -531,8 +533,6 @@ public function getServerVersion(): string
* @param bool $fallbackToMaster whether to return a master connection in case there is no slave connection
* available.
*
* @throws InvalidConfigException
*
* @return Connection the currently active slave connection. `null` is returned if there is no slave available and
* `$fallbackToMaster` is false.
*/
Expand All @@ -557,7 +557,7 @@ public function getSlave(bool $fallbackToMaster = true): ?self
*
* @param bool $fallbackToMaster whether to return a master PDO in case none of the slave connections is available.
*
* @throws Exception|InvalidConfigException
* @throws Exception
*
* @return PDO the PDO instance for the currently active slave connection. `null` is returned if no slave connection
* is available and `$fallbackToMaster` is false.
Expand All @@ -584,6 +584,8 @@ public function getTablePrefix(): string
* @param string $name table name.
* @param bool $refresh whether to reload the table schema even if it is found in the cache.
*
* @throws JsonException
*
* @return TableSchema
*/
public function getTableSchema(string $name, $refresh = false): ?TableSchema
Expand Down Expand Up @@ -800,7 +802,7 @@ protected function openFromPoolSequentially(array $pool): ?self
/* @var $db Connection */
$db = DatabaseFactory::createClass($config);

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

if ($this->schemaCache->isEnabled() && $this->schemaCache->get($key)) {
/** should not try this dead server now */
Expand Down Expand Up @@ -898,6 +900,8 @@ public function quoteTableName(string $name): string
*
* @param int|string $value string to be quoted
*
* @throws Exception|InvalidConfigException
*
* @return int|string the properly quoted string
*
* {@see http://php.net/manual/en/pdo.quote.php}
Expand Down
10 changes: 4 additions & 6 deletions src/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public function refreshTableSchema(string $name): void
$this->tableNames = [];

if ($this->schemaCache->isEnabled()) {
$this->schemaCache->delete($this->getCacheKey($rawName));
$this->schemaCache->remove($this->getCacheKey($rawName));
}
}

Expand Down Expand Up @@ -764,18 +764,16 @@ public function getServerVersion(): string
*
* @throws JsonException
*
* @return string the cache key.
* @return array the cache key.
*/
protected function getCacheKey(string $name): string
protected function getCacheKey(string $name): array
{
$key = [
return [
__CLASS__,
$this->db->getDsn(),
$this->db->getUsername(),
$this->getRawTableName($name),
];

return $this->schemaCache->normalize($key);
}

/**
Expand Down

0 comments on commit c5569e2

Please sign in to comment.