Skip to content

Commit 31bdb17

Browse files
authored
Merge pull request #62 from phpgt/3-constructor-time
feature: configure default cache time in constructor closes #3
2 parents ea4cab8 + 02f0aaf commit 31bdb17

2 files changed

Lines changed: 17 additions & 18 deletions

File tree

src/Cache.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
use TypeError;
88

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

1312
public function __construct(
1413
string $path = "cache",
14+
private readonly int $secondsValid = 60 * 60, // 1 hour of validity by default
1515
?FileAccess $fileAccess = null,
1616
) {
1717
if(is_null($fileAccess)) {
@@ -23,10 +23,9 @@ public function __construct(
2323
public function get(
2424
string $name,
2525
callable $callback,
26-
int $secondsValid = self::DEFAULT_SECONDS_VALID
2726
):mixed {
2827
try {
29-
$this->fileAccess->checkValidity($name, $secondsValid);
28+
$this->fileAccess->checkValidity($name, $this->secondsValid);
3029
return $this->fileAccess->getData($name);
3130
}
3231
catch(FileNotFoundException|CacheInvalidException) {
@@ -36,24 +35,24 @@ public function get(
3635
}
3736
}
3837

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

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

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

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

55-
public function getDateTime(string $name, callable $callback, int $secondsValid = self::DEFAULT_SECONDS_VALID):DateTimeInterface {
56-
$value = $this->get($name, $callback, $secondsValid);
54+
public function getDateTime(string $name, callable $callback):DateTimeInterface {
55+
$value = $this->get($name, $callback);
5756
if($value instanceof DateTimeInterface) {
5857
return $value;
5958
}
@@ -65,8 +64,8 @@ public function getDateTime(string $name, callable $callback, int $secondsValid
6564
return new DateTimeImmutable($value);
6665
}
6766

68-
public function getArray(string $name, callable $callback, int $secondsValid = self::DEFAULT_SECONDS_VALID):array {
69-
$value = $this->get($name, $callback, $secondsValid);
67+
public function getArray(string $name, callable $callback):array {
68+
$value = $this->get($name, $callback);
7069
if(!is_array($value)) {
7170
throw new TypeError("Value with key '$name' is not an array");
7271
}
@@ -79,8 +78,8 @@ public function getArray(string $name, callable $callback, int $secondsValid = s
7978
* @param class-string<T> $className
8079
* @return T
8180
*/
82-
public function getInstance(string $name, string $className, callable $callback, int $secondsValid = self::DEFAULT_SECONDS_VALID):object {
83-
$value = $this->get($name, $callback, $secondsValid);
81+
public function getInstance(string $name, string $className, callable $callback):object {
82+
$value = $this->get($name, $callback);
8483
if(get_class($value) !== $className) {
8584
throw new TypeError("Value is not of type $className");
8685
}

test/phpunit/CacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private function getSut(array $mockFiles = []):Cache {
126126
}
127127
return new Cache(
128128
sys_get_temp_dir() . "/phpgt-filecache",
129-
$mockFileAccess,
129+
fileAccess: $mockFileAccess,
130130
);
131131
}
132132
}

0 commit comments

Comments
 (0)