File tree Expand file tree Collapse file tree 5 files changed +79
-11
lines changed Expand file tree Collapse file tree 5 files changed +79
-11
lines changed Original file line number Diff line number Diff line change 2
2
namespace Gt \FileCache ;
3
3
4
4
class Cache {
5
+ private FileAccess $ fileAccess ;
6
+
5
7
public function __construct (
6
- private string $ path = "cache "
8
+ string $ path = "cache " ,
9
+ ?FileAccess $ fileAccess = null ,
7
10
) {
8
- if (! is_dir ( $ path )) {
9
- mkdir ( $ path , 0775 , true );
11
+ if (is_null ( $ fileAccess )) {
12
+ $ fileAccess = new FileAccess ( $ path );
10
13
}
14
+ $ this ->fileAccess = $ fileAccess ;
11
15
}
12
16
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 ;
17
30
}
18
-
19
- $ value = $ callback ();
20
- file_put_contents ($ fileName , serialize ($ value ));
21
- return $ value ;
22
31
}
23
32
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+ namespace Gt \FileCache ;
3
+
4
+ class CacheInvalidException extends FileCacheException {
5
+
6
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+ namespace Gt \FileCache ;
3
+
4
+ use RuntimeException ;
5
+
6
+ class FileCacheException extends RuntimeException {
7
+
8
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+ namespace Gt \FileCache ;
3
+
4
+ class FileNotFoundException extends FileCacheException {
5
+ public function __construct (string $ name ) {
6
+ }
7
+ }
You can’t perform that action at this time.
0 commit comments