diff --git a/composer.json b/composer.json index fc013967d8d..f82e52a7873 100644 --- a/composer.json +++ b/composer.json @@ -79,7 +79,8 @@ "bower-asset/inputmask": "~3.2.2 | ~3.3.5", "bower-asset/punycode": "1.3.*", "bower-asset/yii2-pjax": "~2.0.1", - "paragonie/random_compat": ">=1" + "paragonie/random_compat": ">=1", + "yiisoft/cache": "^3.0" }, "require-dev": { "dms/phpunit-arraysubset-asserts": "^0.5", diff --git a/composer.lock b/composer.lock index 80fc9217a2b..08ee6928e0c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1c9e9e5bc6be7b83ef45eed0425bd5b9", + "content-hash": "e845f3e1cb541d8fb639b7e771c89d43", "packages": [ { "name": "bower-asset/inputmask", @@ -256,6 +256,142 @@ }, "time": "2020-10-15T08:29:30+00:00" }, + { + "name": "psr/simple-cache", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/simple-cache.git", + "reference": "764e0b3939f5ca87cb904f570ef9be2d78a07865" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/simple-cache/zipball/764e0b3939f5ca87cb904f570ef9be2d78a07865", + "reference": "764e0b3939f5ca87cb904f570ef9be2d78a07865", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\SimpleCache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interfaces for simple caching", + "keywords": [ + "cache", + "caching", + "psr", + "psr-16", + "simple-cache" + ], + "support": { + "source": "https://github.com/php-fig/simple-cache/tree/3.0.0" + }, + "time": "2021-10-29T13:26:27+00:00" + }, + { + "name": "yiisoft/cache", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/yiisoft/cache.git", + "reference": "67ae4b40d3aefd193dfd9a3f458f3e145adb0d84" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/yiisoft/cache/zipball/67ae4b40d3aefd193dfd9a3f458f3e145adb0d84", + "reference": "67ae4b40d3aefd193dfd9a3f458f3e145adb0d84", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-mbstring": "*", + "php": "^8.0", + "psr/simple-cache": "^2.0|^3.0" + }, + "provide": { + "psr/simple-cache-implementation": "2.0|3.0" + }, + "require-dev": { + "maglnet/composer-require-checker": "^4.2", + "phpunit/phpunit": "^9.5", + "rector/rector": "^0.15.2", + "roave/infection-static-analysis-plugin": "^1.16", + "spatie/phpunit-watcher": "^1.23", + "vimeo/psalm": "^4.30|^5.6", + "yiisoft/di": "^1.2" + }, + "suggest": { + "yiisoft/cache-apcu": "Allows to store cache using APCu PECL extension", + "yiisoft/cache-db": "Allows storing cache to the database", + "yiisoft/cache-file": "Allows storing cache to the files", + "yiisoft/cache-memcached": "Allows to store cache using Memcached PECL extension", + "yiisoft/cache-redis": "Allows storing cache to the Redis", + "yiisoft/cache-wincache": "Allows to store cache using WinCache PECL extension" + }, + "type": "library", + "extra": { + "config-plugin-options": { + "source-directory": "config" + }, + "config-plugin": { + "di": "di.php" + } + }, + "autoload": { + "psr-4": { + "Yiisoft\\Cache\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "description": "Yii Caching Library", + "homepage": "https://www.yiiframework.com/", + "keywords": [ + "cache", + "psr-16", + "yii" + ], + "support": { + "chat": "https://t.me/yii3en", + "forum": "https://www.yiiframework.com/forum/", + "irc": "irc://irc.freenode.net/yii", + "issues": "https://github.com/yiisoft/cache/issues?state=open", + "source": "https://github.com/yiisoft/cache", + "wiki": "https://www.yiiframework.com/wiki/" + }, + "funding": [ + { + "url": "https://github.com/sponsors/yiisoft", + "type": "github" + }, + { + "url": "https://opencollective.com/yiisoft", + "type": "opencollective" + } + ], + "time": "2023-02-15T14:30:56+00:00" + }, { "name": "yiisoft/yii2-composer", "version": "2.0.10", diff --git a/framework/caching/ArrayCache.php b/framework/caching/ArrayCache.php index d8fdaf61b18..a693611188e 100644 --- a/framework/caching/ArrayCache.php +++ b/framework/caching/ArrayCache.php @@ -1,4 +1,5 @@ _cache = new \Yiisoft\Cache\ArrayCache(); + } /** @@ -33,7 +42,7 @@ class ArrayCache extends Cache public function exists($key) { $key = $this->buildKey($key); - return isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true)); + return $this->_cache->get($key, null) != null; } /** @@ -41,11 +50,7 @@ public function exists($key) */ protected function getValue($key) { - if (isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true))) { - return $this->_cache[$key][0]; - } - - return false; + return $this->_cache->get($key, false); } /** @@ -53,8 +58,7 @@ protected function getValue($key) */ protected function setValue($key, $value, $duration) { - $this->_cache[$key] = [$value, $duration === 0 ? 0 : microtime(true) + $duration]; - return true; + return $this->_cache->set($key, $value, $duration); } /** @@ -62,10 +66,11 @@ protected function setValue($key, $value, $duration) */ protected function addValue($key, $value, $duration) { - if (isset($this->_cache[$key]) && ($this->_cache[$key][1] === 0 || $this->_cache[$key][1] > microtime(true))) { + if ($this->_cache->get($key, null) != null) { return false; } - $this->_cache[$key] = [$value, $duration === 0 ? 0 : microtime(true) + $duration]; + + $this->_cache->set($key, $value, $duration); return true; } @@ -74,8 +79,7 @@ protected function addValue($key, $value, $duration) */ protected function deleteValue($key) { - unset($this->_cache[$key]); - return true; + return $this->_cache->delete($key); } /** @@ -83,7 +87,6 @@ protected function deleteValue($key) */ protected function flushValues() { - $this->_cache = []; - return true; + return $this->_cache->clear(); } }