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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Cache data in local files.

Making expensive operations like HTTP calls or database queries can be minimised by caching the result of the operations in local files. This can improve performance, reduce network usage, and avoid rate limiting, to name some common benefits. This library provides a single function to define where to cache, what to cache, and when to cache. A validity of 1 hour is set by default, if the valid time is not specified.

Pass `secondsValid: 0` to the `Cache` constructor to cache indefinitely. The value
is kept until you call `invalidate($name)` or remove its cache file.

***

<a href="https://github.com/PhpGt/FileCache/actions" target="_blank">
Expand Down
3 changes: 3 additions & 0 deletions src/FileAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function checkValidity(string $name, int $secondsValidity):void {
if(!is_file($filePath)) {
throw new CacheInvalidException("$filePath (does not exist)");
}
if($secondsValidity === 0) {
return;
}
$fileModifiedTimestamp = filemtime($filePath);
if($fileModifiedTimestamp <= time() - $secondsValidity) {
throw new CacheInvalidException($filePath);
Expand Down
38 changes: 38 additions & 0 deletions test/phpunit/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,44 @@ public function testGet_multipleCallsDoesNotCallbackMultipleTimes():void {
self::assertSame(1, $count);
}

public function testGet_zeroValidityCachesUntilInvalidated():void {
$sut = new Cache(sys_get_temp_dir() . "/phpgt-filecache", 0);
$count = 0;
$callback = function()use(&$count):int {
return ++$count;
};

self::assertSame(1, $sut->get("indefinite", $callback));
self::assertSame(1, $sut->get("indefinite", $callback));
self::assertSame(1, $count);

$sut->invalidate("indefinite");
self::assertSame(2, $sut->get("indefinite", $callback));
}

public function testGet_zeroValidityReadsOldCacheFromAnotherInstance():void {
$path = sys_get_temp_dir() . "/phpgt-filecache";
$sut = new Cache($path, 0);
self::assertSame("original", $sut->get("indefinite", fn() => "original"));
self::assertTrue(touch("$path/indefinite", time() - 31536000));
clearstatcache();

$sut = new Cache($path, 0);
self::assertSame("original", $sut->get("indefinite", function():never {
self::fail("An indefinite cache must not regenerate an old value.");
}));
}

public function testGet_positiveValidityRegeneratesExpiredCache():void {
$path = sys_get_temp_dir() . "/phpgt-filecache";
$sut = new Cache($path, 60);
self::assertSame("original", $sut->get("expired", fn() => "original"));
self::assertTrue(touch("$path/expired", time() - 120));
clearstatcache();

self::assertSame("replacement", $sut->get("expired", fn() => "replacement"));
}

public function testGet_nullValueCanBeCached():void {
$sut = $this->getSut();
$count = 0;
Expand Down
Loading