1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Suricate\Cache; |
6
|
|
|
|
7
|
|
|
use Suricate; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* File cache extension for Suricate |
11
|
|
|
* |
12
|
|
|
* @package Suricate |
13
|
|
|
* @author Mathieu LESNIAK <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @property string $path Storage path (default: app/storage/app) |
16
|
|
|
* @property int $defaultExpiry Key default expiry in sec |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
class File extends Suricate\Cache |
20
|
|
|
{ |
21
|
|
|
protected $parametersList = ['path', 'defaultExpiry']; |
22
|
|
|
private $handler; |
23
|
|
|
|
24
|
2 |
|
public function __construct() |
25
|
|
|
{ |
26
|
2 |
|
parent::__construct(); |
27
|
|
|
|
28
|
2 |
|
$this->handler = false; |
29
|
2 |
|
$this->path = app_path() . '/storage/app/'; |
30
|
2 |
|
$this->defaultExpiry = 3600; |
31
|
2 |
|
} |
32
|
|
|
|
33
|
1 |
|
public function getDefaultExpiry() |
34
|
|
|
{ |
35
|
1 |
|
return $this->defaultExpiry; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function setDefaultExpiry($expiry) |
39
|
|
|
{ |
40
|
1 |
|
$this->defaultExpiry = $expiry; |
41
|
|
|
|
42
|
1 |
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Put a value into memcache |
47
|
|
|
* @param string $variable Variable name |
48
|
|
|
* @param mixed $value Value |
49
|
|
|
* @param int $expiry Cache expiry |
50
|
|
|
*/ |
51
|
|
|
public function set(string $variable, $value, $expiry = null) |
52
|
|
|
{ |
53
|
|
|
if ($expiry === null) { |
54
|
|
|
$expiry = $this->defaultExpiry; |
55
|
|
|
} |
56
|
|
|
$fp = fopen($this->path . $variable, 'w'); |
57
|
|
|
if ($fp === false) { |
58
|
|
|
throw new \Exception( |
59
|
|
|
"Cannot open cache file " . $this->path . $variable |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
fputs($fp, $value); |
63
|
|
|
fclose($fp); |
64
|
|
|
if ($expiry !== null) { |
|
|
|
|
65
|
|
|
$fp = fopen($this->path . $variable . '.expiry', 'w'); |
66
|
|
|
if ($fp === false) { |
67
|
|
|
throw new \Exception( |
68
|
|
|
"Cannot open cache file " . |
69
|
|
|
$this->path . |
70
|
|
|
$variable . |
71
|
|
|
'.expiry' |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
fputs($fp, (string) (time() + $expiry)); |
75
|
|
|
fclose($fp); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function get(string $variable) |
80
|
|
|
{ |
81
|
|
|
if (is_readable($this->path . $variable)) { |
82
|
|
|
if (is_readable($this->path . $variable . '.expiry')) { |
83
|
|
|
$expiry = file_get_contents( |
84
|
|
|
$this->path . $variable . '.expiry' |
85
|
|
|
); |
86
|
|
|
$hasExpired = time() - (int) $expiry > 0 ? 1 : -1; |
87
|
|
|
} else { |
88
|
|
|
$hasExpired = 0; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($hasExpired < 0) { |
92
|
|
|
return file_get_contents($this->path . $variable); |
93
|
|
|
} elseif ($hasExpired > 0) { |
94
|
|
|
unlink($this->path . $variable . '.expiry'); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function delete(string $variable) |
101
|
|
|
{ |
102
|
|
|
if (is_file($this->path . $variable)) { |
103
|
|
|
return unlink($this->path . $variable); |
104
|
|
|
} |
105
|
|
|
if (is_file($this->path . $variable . '.expiry')) { |
106
|
|
|
return unlink($this->path . $variable . '.expiry'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|