Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 3.1.1 under development

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

## 3.1.0 October 09, 2023

Expand Down
9 changes: 7 additions & 2 deletions src/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
*/
public function __construct(
private string $cachePath,
private int $directoryMode = 0775,

Check warning on line 92 in src/FileCache.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ * * @throws CacheException If failed to create cache directory. */ - public function __construct(private string $cachePath, private int $directoryMode = 0775) + public function __construct(private string $cachePath, private int $directoryMode = 510) { if (!$this->createDirectoryIfNotExists($cachePath)) { throw new CacheException("Failed to create cache directory \"{$cachePath}\".");
) {
if (!$this->createDirectoryIfNotExists($cachePath)) {
throw new CacheException("Failed to create cache directory \"$cachePath\".");
Expand Down Expand Up @@ -132,10 +132,10 @@
throw new CacheException("Failed to create cache directory \"$cacheDirectory\".");
}

// If ownership differs the touch call will fail, so we try to
// If ownership differs, the touch call will fail, so we try to
// rebuild the file from scratch by deleting it first
// https://github.com/yiisoft/yii2/pull/16120
if (function_exists('posix_geteuid') && is_file($file) && fileowner($file) !== posix_geteuid()) {

Check warning on line 138 in src/FileCache.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndAllSubExprNegation": --- Original +++ New @@ @@ // If ownership differs, the touch call will fail, so we try to // rebuild the file from scratch by deleting it first // https://github.com/yiisoft/yii2/pull/16120 - if (function_exists('posix_geteuid') && is_file($file) && fileowner($file) !== posix_geteuid()) { + if (!function_exists('posix_geteuid') && !is_file($file) && !(fileowner($file) !== posix_geteuid())) { @Unlink($file); } if (file_put_contents($file, serialize($value), LOCK_EX) === false) {
@unlink($file);
}

Expand All @@ -150,7 +150,12 @@
}
}

$result = @touch($file, $expiration);
$result = false;

if (@touch($file, $expiration)) {
clearstatcache();
$result = true;
}

return $this->isLastErrorSafe($result);
}
Expand Down Expand Up @@ -197,7 +202,7 @@
$this->set((string) $key, $value, $ttl);
}

return true;

Check warning on line 205 in src/FileCache.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ foreach ($values as $key => $value) { $this->set((string) $key, $value, $ttl); } - return true; + return false; } public function deleteMultiple(iterable $keys) : bool {
}

public function deleteMultiple(iterable $keys): bool
Expand All @@ -209,7 +214,7 @@
$this->delete($key);
}

return true;

Check warning on line 217 in src/FileCache.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ foreach ($keys as $key) { $this->delete($key); } - return true; + return false; } public function has(string $key) : bool {
}

public function has(string $key): bool
Expand Down Expand Up @@ -332,7 +337,7 @@
return true;
}

$result = !is_file($path) && mkdir(directory: $path, recursive: true) && is_dir($path);

Check warning on line 340 in src/FileCache.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ if (is_dir($path)) { return true; } - $result = !is_file($path) && mkdir(directory: $path, recursive: true) && is_dir($path); + $result = !is_file($path) && mkdir(directory: $path, recursive: true) || is_dir($path); if ($result) { chmod($path, $this->directoryMode); }

if ($result) {
chmod($path, $this->directoryMode);
Expand Down
18 changes: 18 additions & 0 deletions tests/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,4 +647,22 @@ private function removeDirectory(string $directory): void

rmdir($directory);
}

public function testSetClearsStatCache(): void
{
$this->cache->set(__FUNCTION__, 'cache1', 2);

$refClass = new \ReflectionClass($this->cache);
$refMethodGetCacheFile = $refClass->getMethod('getCacheFile');
$refMethodGetCacheFile->setAccessible(true);
$cacheFile = $refMethodGetCacheFile->invoke($this->cache, __FUNCTION__);

// simulate cache expire 10 seconds ago
touch($cacheFile, time() - 10);
clearstatcache();

$this->assertNull($this->cache->get(__FUNCTION__));
$this->assertTrue($this->cache->set(__FUNCTION__, 'cache2', 2));
$this->assertSame('cache2', $this->cache->get(__FUNCTION__));
}
}
Loading