diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a4c695..a58f730 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ - **layout:** Add anti-robots html meta tag (9c21e73) ### Fix +- **API:** ApiMiddleware not work after change Response Component (6da82a7) - **Bencode:** Fix dict keys may not in sorted order (81f0783) - **Config:** Fix JSON type config return False (1129008) - **Cron:** Fix components lost in CronTabProcess (1ced4bf) diff --git a/framework/Utils/ClassValueCacheUtils.php b/framework/Utils/ClassValueCacheUtils.php index e30c4ce..3937f6e 100644 --- a/framework/Utils/ClassValueCacheUtils.php +++ b/framework/Utils/ClassValueCacheUtils.php @@ -16,19 +16,27 @@ protected function getCacheNameSpace(): string } // Get from class, redis cache, generate closure (may database) and then cache it in class and redis cache - final protected function getCacheValue($key, $closure) + final protected function getCacheValue(string $key, callable $closure, int $ttl = null) { if (!isset($this->$key)) { - $this->$key = app()->redis->hGet($this->getCacheNameSpace(), $key); - if (false === $this->$key) { + /** @var array $key_from_cache */ + $key_from_cache = app()->redis->hGet($this->getCacheNameSpace(), $key); + if (false === $key_from_cache || ($key_from_cache['expire'] ?? -1) > time()) { $this->$key = $closure(); - app()->redis->hSet($this->getCacheNameSpace(), $key, $this->$key); + $cache = ['data' => $this->$key]; + if (!is_null($ttl)) { + $cache['expire'] = time() + $ttl; + } + app()->redis->hSet($this->getCacheNameSpace(), $key, $cache); + } else { + $this->$key = $key_from_cache['data']; } } return $this->$key; } - final protected function removeCacheValue($key) { + final protected function removeCacheValue($key) + { unset($this->$key); app()->redis->hDel($this->getCacheNameSpace(), $key); }