diff --git a/composer.json b/composer.json index c435c51..95d1bdf 100644 --- a/composer.json +++ b/composer.json @@ -18,9 +18,8 @@ "ext-sockets": "*", "doctrine/collections": "^1.3", "doctrine/dbal": "^3.0", - "psr/simple-cache": "^1.0", - "symfony/dependency-injection": "^3.1|^4.0|^5.0", - "symfony/event-dispatcher": "^3.1|^4.0|^5.0" + "psr/simple-cache": "^1.0 | ^3.0", + "symfony/event-dispatcher": "^3.1|^4.0|^5.0|^6.0" }, "require-dev": { "phpunit/phpunit": "^9.0" diff --git a/src/MySQLReplication/Cache/ArrayCache.php b/src/MySQLReplication/Cache/ArrayCache.php index 3c210c8..65fd720 100644 --- a/src/MySQLReplication/Cache/ArrayCache.php +++ b/src/MySQLReplication/Cache/ArrayCache.php @@ -3,11 +3,68 @@ namespace MySQLReplication\Cache; -use MySQLReplication\Config\Config; +use Composer\InstalledVersions; use Psr\SimpleCache\CacheInterface; -class ArrayCache implements CacheInterface -{ +if (class_exists(InstalledVersions::class) && version_compare(InstalledVersions::getVersion("psr/simple-cache"), '3.0.0', ">=")) { + class ArrayCache implements CacheInterface + { + use ArrayCacheTrait; + + /** + * @inheritDoc + */ + public function get(string $key, $default = null): mixed + { + return $this->has($key) ? $this->tableMapCache[$key] : $default; + } + + /** + * @inheritDoc + */ + public function getMultiple(iterable $keys, $default = null): iterable + { + $data = []; + foreach ($keys as $key) { + if ($this->has($key)) { + $data[$key] = $this->tableMapCache[$key]; + } + } + + return [] !== $data ? $data : $default; + } + } +} else { + class ArrayCache implements CacheInterface + { + use ArrayCacheTrait; + + /** + * @inheritDoc + */ + public function get($key, $default = null) + { + return $this->has($key) ? $this->tableMapCache[$key] : $default; + } + + /** + * @inheritDoc + */ + public function getMultiple($keys, $default = null) + { + $data = []; + foreach ($keys as $key) { + if ($this->has($key)) { + $data[$key] = $this->tableMapCache[$key]; + } + } + + return [] !== $data ? $data : $default; + } + } +} + +trait ArrayCacheTrait { private $tableMapCache = []; /** @@ -20,13 +77,6 @@ public function __construct(int $tableCacheSize = 128) $this->tableCacheSize = $tableCacheSize; } - /** - * @inheritDoc - */ - public function get($key, $default = null) - { - return $this->has($key) ? $this->tableMapCache[$key] : $default; - } /** * @inheritDoc @@ -46,21 +96,6 @@ public function clear(): bool return true; } - /** - * @inheritDoc - */ - public function getMultiple($keys, $default = null) - { - $data = []; - foreach ($keys as $key) { - if ($this->has($key)) { - $data[$key] = $this->tableMapCache[$key]; - } - } - - return [] !== $data ? $data : $default; - } - /** * @inheritDoc */ @@ -114,4 +149,4 @@ public function delete($key): bool return true; } -} \ No newline at end of file +}