Skip to content

Commit 9735336

Browse files
author
Greg Bowler
committed
feature: basic functionality
1 parent ec42a11 commit 9735336

File tree

5 files changed

+79
-11
lines changed

5 files changed

+79
-11
lines changed

src/Cache.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,31 @@
22
namespace Gt\FileCache;
33

44
class Cache {
5+
private FileAccess $fileAccess;
6+
57
public function __construct(
6-
private string $path = "cache"
8+
string $path = "cache",
9+
?FileAccess $fileAccess = null,
710
) {
8-
if(!is_dir($path)) {
9-
mkdir($path, 0775, true);
11+
if(is_null($fileAccess)) {
12+
$fileAccess = new FileAccess($path);
1013
}
14+
$this->fileAccess = $fileAccess;
1115
}
1216

13-
public function get(string $name, callable $callback):mixed {
14-
$fileName = $this->path . "/$name";
15-
if(file_exists($fileName)) {
16-
return unserialize(file_get_contents($fileName));
17+
public function get(
18+
string $name,
19+
callable $callback,
20+
int $secondsValid = 60 * 60 // 1 hour of validity
21+
):mixed {
22+
try {
23+
$this->fileAccess->checkValidity($name, $secondsValid);
24+
return $this->fileAccess->getData($name);
25+
}
26+
catch(FileNotFoundException|CacheInvalidException) {
27+
$value = $callback();
28+
$this->fileAccess->setData($name, $value);
29+
return $value;
1730
}
18-
19-
$value = $callback();
20-
file_put_contents($fileName, serialize($value));
21-
return $value;
2231
}
2332
}

src/CacheInvalidException.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace Gt\FileCache;
3+
4+
class CacheInvalidException extends FileCacheException {
5+
6+
}

src/FileAccess.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
namespace Gt\FileCache;
3+
4+
class FileAccess {
5+
public function __construct(
6+
private readonly string $dirPath
7+
) {
8+
}
9+
10+
public function getData(string $name):mixed {
11+
$filePath = "$this->dirPath/$name";
12+
if(!is_file($filePath)) {
13+
throw new FileNotFoundException($filePath);
14+
}
15+
16+
$contents = file_get_contents($filePath);
17+
return unserialize($contents);
18+
}
19+
20+
public function setData(string $name, mixed $value):void {
21+
if(!is_dir($this->dirPath)) {
22+
mkdir($this->dirPath, 0775, true);
23+
}
24+
$filePath = "$this->dirPath/$name";
25+
file_put_contents($filePath, serialize($value));
26+
}
27+
28+
public function checkValidity(string $name, int $secondsValidity):void {
29+
$filePath = "$this->dirPath/$name";
30+
if(!is_file($filePath)) {
31+
throw new CacheInvalidException("$filePath (does not exist)");
32+
}
33+
$fileModifiedTimestamp = filemtime($filePath);
34+
if($fileModifiedTimestamp <= time() - $secondsValidity) {
35+
throw new CacheInvalidException($filePath);
36+
}
37+
}
38+
}

src/FileCacheException.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
namespace Gt\FileCache;
3+
4+
use RuntimeException;
5+
6+
class FileCacheException extends RuntimeException {
7+
8+
}

src/FileNotFoundException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Gt\FileCache;
3+
4+
class FileNotFoundException extends FileCacheException {
5+
public function __construct(string $name) {
6+
}
7+
}

0 commit comments

Comments
 (0)