Skip to content

feature: configure default cache time in constructor closes #3 #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
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
33 changes: 16 additions & 17 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use TypeError;

class Cache implements CallbackTypeSafeGetter {
const DEFAULT_SECONDS_VALID = 60 * 60; // 1 hour of validity.
private FileAccess $fileAccess;

public function __construct(
string $path = "cache",
private readonly int $secondsValid = 60 * 60, // 1 hour of validity by default
?FileAccess $fileAccess = null,
) {
if(is_null($fileAccess)) {
Expand All @@ -23,10 +23,9 @@ public function __construct(
public function get(
string $name,
callable $callback,
int $secondsValid = self::DEFAULT_SECONDS_VALID
):mixed {
try {
$this->fileAccess->checkValidity($name, $secondsValid);
$this->fileAccess->checkValidity($name, $this->secondsValid);
return $this->fileAccess->getData($name);
}
catch(FileNotFoundException|CacheInvalidException) {
Expand All @@ -36,24 +35,24 @@ public function get(
}
}

public function getString(string $name, callable $callback, int $secondsValid = self::DEFAULT_SECONDS_VALID):string {
return (string)$this->get($name, $callback, $secondsValid);
public function getString(string $name, callable $callback):string {
return (string)$this->get($name, $callback);
}

public function getInt(string $name, callable $callback, int $secondsValid = self::DEFAULT_SECONDS_VALID):int {
return (int)$this->get($name, $callback, $secondsValid);
public function getInt(string $name, callable $callback):int {
return (int)$this->get($name, $callback);
}

public function getFloat(string $name, callable $callback, int $secondsValid = self::DEFAULT_SECONDS_VALID):float {
return (float)$this->get($name, $callback, $secondsValid);
public function getFloat(string $name, callable $callback):float {
return (float)$this->get($name, $callback);
}

public function getBool(string $name, callable $callback, int $secondsValid = self::DEFAULT_SECONDS_VALID):bool {
return (bool)$this->get($name, $callback, $secondsValid);
public function getBool(string $name, callable $callback):bool {
return (bool)$this->get($name, $callback);
}

public function getDateTime(string $name, callable $callback, int $secondsValid = self::DEFAULT_SECONDS_VALID):DateTimeInterface {
$value = $this->get($name, $callback, $secondsValid);
public function getDateTime(string $name, callable $callback):DateTimeInterface {
$value = $this->get($name, $callback);
if($value instanceof DateTimeInterface) {
return $value;
}
Expand All @@ -65,8 +64,8 @@ public function getDateTime(string $name, callable $callback, int $secondsValid
return new DateTimeImmutable($value);
}

public function getArray(string $name, callable $callback, int $secondsValid = self::DEFAULT_SECONDS_VALID):array {
$value = $this->get($name, $callback, $secondsValid);
public function getArray(string $name, callable $callback):array {
$value = $this->get($name, $callback);
if(!is_array($value)) {
throw new TypeError("Value with key '$name' is not an array");
}
Expand All @@ -79,8 +78,8 @@ public function getArray(string $name, callable $callback, int $secondsValid = s
* @param class-string<T> $className
* @return T
*/
public function getInstance(string $name, string $className, callable $callback, int $secondsValid = self::DEFAULT_SECONDS_VALID):object {
$value = $this->get($name, $callback, $secondsValid);
public function getInstance(string $name, string $className, callable $callback):object {
$value = $this->get($name, $callback);
if(get_class($value) !== $className) {
throw new TypeError("Value is not of type $className");
}
Expand Down
2 changes: 1 addition & 1 deletion test/phpunit/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private function getSut(array $mockFiles = []):Cache {
}
return new Cache(
sys_get_temp_dir() . "/phpgt-filecache",
$mockFileAccess,
fileAccess: $mockFileAccess,
);
}
}