Skip to content

Commit 8fde6e4

Browse files
authored
Fix #85: Clear stat cache in FileCache::set()
1 parent fcbee8d commit 8fde6e4

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 3.1.1 under development
44

5-
- no changes in this release.
5+
- Bug #85: Clear stat cache in `FileCache::set()` (@samdark)
66

77
## 3.1.0 October 09, 2023
88

src/FileCache.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function set(string $key, mixed $value, null|int|DateInterval $ttl = null
132132
throw new CacheException("Failed to create cache directory \"$cacheDirectory\".");
133133
}
134134

135-
// If ownership differs the touch call will fail, so we try to
135+
// If ownership differs, the touch call will fail, so we try to
136136
// rebuild the file from scratch by deleting it first
137137
// https://github.com/yiisoft/yii2/pull/16120
138138
if (function_exists('posix_geteuid') && is_file($file) && fileowner($file) !== posix_geteuid()) {
@@ -150,7 +150,12 @@ public function set(string $key, mixed $value, null|int|DateInterval $ttl = null
150150
}
151151
}
152152

153-
$result = @touch($file, $expiration);
153+
$result = false;
154+
155+
if (@touch($file, $expiration)) {
156+
clearstatcache();
157+
$result = true;
158+
}
154159

155160
return $this->isLastErrorSafe($result);
156161
}

tests/FileCacheTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,4 +647,22 @@ private function removeDirectory(string $directory): void
647647

648648
rmdir($directory);
649649
}
650+
651+
public function testSetClearsStatCache(): void
652+
{
653+
$this->cache->set(__FUNCTION__, 'cache1', 2);
654+
655+
$refClass = new \ReflectionClass($this->cache);
656+
$refMethodGetCacheFile = $refClass->getMethod('getCacheFile');
657+
$refMethodGetCacheFile->setAccessible(true);
658+
$cacheFile = $refMethodGetCacheFile->invoke($this->cache, __FUNCTION__);
659+
660+
// simulate cache expire 10 seconds ago
661+
touch($cacheFile, time() - 10);
662+
clearstatcache();
663+
664+
$this->assertNull($this->cache->get(__FUNCTION__));
665+
$this->assertTrue($this->cache->set(__FUNCTION__, 'cache2', 2));
666+
$this->assertSame('cache2', $this->cache->get(__FUNCTION__));
667+
}
650668
}

0 commit comments

Comments
 (0)