From a8586fc2961a4a6c7df2160d80493b5e13bc55be Mon Sep 17 00:00:00 2001 From: Robert Korulczyk Date: Mon, 30 Dec 2024 17:53:25 +0100 Subject: [PATCH] Fix #20300: Clear stat cache in `FileCache::setValue()` --- framework/CHANGELOG.md | 1 + framework/caching/FileCache.php | 7 ++++++- tests/framework/caching/FileCacheTest.php | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 5d194e4316b..b79a650fa27 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -23,6 +23,7 @@ Yii Framework 2 Change Log - New #20185: Add `BackedEnum` support to `AttributeTypecastBehavior` (briedis) - Bug #17365: Fix "Trying to access array offset on null" warning (xcopy) - Bug #20296: Fix broken enum test (briedis) +- Bug #20300: Clear stat cache in `FileCache::setValue()` (rob006) 2.0.51 July 18, 2024 -------------------- diff --git a/framework/caching/FileCache.php b/framework/caching/FileCache.php index 864299f840f..5eb8a5ab9ab 100644 --- a/framework/caching/FileCache.php +++ b/framework/caching/FileCache.php @@ -155,7 +155,12 @@ protected function setValue($key, $value, $duration) $duration = 31536000; // 1 year } - return @touch($cacheFile, $duration + time()); + if (@touch($cacheFile, $duration + time())) { + clearstatcache(); + return true; + } + + return false; } $message = "Unable to write cache file '{$cacheFile}'"; diff --git a/tests/framework/caching/FileCacheTest.php b/tests/framework/caching/FileCacheTest.php index aa12bf9cc13..868bae40c85 100644 --- a/tests/framework/caching/FileCacheTest.php +++ b/tests/framework/caching/FileCacheTest.php @@ -82,4 +82,24 @@ public function testKeyPrefix() $this->assertTrue(is_dir(dirname($cacheFile)), 'File not found ' . $cacheFile); $this->assertEquals($value, $refMethodGet->invoke($cache, $key)); } + + public function testStatCache() + { + $cache = $this->getCacheInstance(); + $cache->set(__FUNCTION__, 'cache1', 2); + + $normalizeKey = $cache->buildKey(__FUNCTION__); + $refClass = new \ReflectionClass($cache); + $refMethodGetCacheFile = $refClass->getMethod('getCacheFile'); + $refMethodGetCacheFile->setAccessible(true); + $cacheFile = $refMethodGetCacheFile->invoke($cache, $normalizeKey); + + // simulate cache expire 10 seconds ago + touch($cacheFile, time() - 10); + clearstatcache(); + + $this->assertFalse($cache->get(__FUNCTION__)); + $this->assertTrue($cache->set(__FUNCTION__, 'cache2', 2)); + $this->assertSame('cache2', $cache->get(__FUNCTION__)); + } }