Skip to content

Commit

Permalink
Fix #20300: Clear stat cache in FileCache::setValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
rob006 authored Dec 30, 2024
1 parent b4efeb3 commit a8586fc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------
Expand Down
7 changes: 6 additions & 1 deletion framework/caching/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}'";
Expand Down
20 changes: 20 additions & 0 deletions tests/framework/caching/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__));
}
}

0 comments on commit a8586fc

Please sign in to comment.