1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2022-2023 Rafael San José Tovar <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This program is free software; you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Alxarafe\Core\Singletons; |
20
|
|
|
|
21
|
|
|
use Alxarafe\Core\Utils\Utils; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Simple file cache |
25
|
|
|
*/ |
26
|
|
|
abstract class PhpFileCache |
27
|
|
|
{ |
28
|
|
|
private static $config; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* php_file_cache constructor. |
32
|
|
|
*/ |
33
|
|
|
public static function load() |
34
|
|
|
{ |
35
|
|
|
self::$config = [ |
36
|
|
|
'cache_path' => constant('TMP_DIR') . 'cache', |
37
|
|
|
'expires' => 60 * 24 * 365, |
38
|
|
|
]; |
39
|
|
|
Utils::createDir(self::$config['cache_path']); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the data associated with a key |
44
|
|
|
* |
45
|
|
|
* @access public |
46
|
|
|
* |
47
|
|
|
* @param string $key |
48
|
|
|
* |
49
|
|
|
* @return mixed the content you put in, or null if expired or not found |
50
|
|
|
*/ |
51
|
|
|
public function get($key, $raw = false, $custom_time = null) |
52
|
|
|
{ |
53
|
|
|
if (!$this->fileExpired($file = $this->getRoute($key), $custom_time)) { |
54
|
|
|
$content = file_get_contents($file); |
55
|
|
|
return $raw ? $content : unserialize($content); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check if a file has expired or not. |
63
|
|
|
* |
64
|
|
|
* @access public |
65
|
|
|
* |
66
|
|
|
* @param string $file the rout to the file |
67
|
|
|
* @param int $time the number of minutes it was set to expire |
68
|
|
|
* |
69
|
|
|
* @return bool if the file has expired or not |
70
|
|
|
*/ |
71
|
|
|
public function fileExpired($file, $time = null) |
72
|
|
|
{ |
73
|
|
|
if (file_exists($file)) { |
74
|
|
|
return (time() > (filemtime($file) + 60 * ($time ?? self::$config['expires']))); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get a route to the file associated to that key. |
82
|
|
|
* |
83
|
|
|
* @access public |
84
|
|
|
* |
85
|
|
|
* @param string $key |
86
|
|
|
* |
87
|
|
|
* @return string the filename of the php file |
88
|
|
|
*/ |
89
|
|
|
public function getRoute($key) |
90
|
|
|
{ |
91
|
|
|
return self::$config['cache_path'] . '/' . md5($key) . '.php'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Put content into the cache |
96
|
|
|
* |
97
|
|
|
* @access public |
98
|
|
|
* |
99
|
|
|
* @param string $key |
100
|
|
|
* @param mixed $content the the content you want to store |
101
|
|
|
* @param bool $raw whether if you want to store raw data or not. If it is true, $content *must* be a string |
102
|
|
|
* |
103
|
|
|
* @return bool whether if the operation was successful or not |
104
|
|
|
*/ |
105
|
|
|
public function put($key, $content, $raw = false) |
106
|
|
|
{ |
107
|
|
|
$dest_file_name = $this->getRoute($key); |
108
|
|
|
/** Use a unique temporary filename to make writes atomic with rewrite */ |
109
|
|
|
$temp_file_name = str_replace(".php", uniqid("-", true) . ".php", $dest_file_name); |
110
|
|
|
$ret = @file_put_contents($temp_file_name, $raw ? $content : serialize($content)); |
111
|
|
|
if ($ret !== false) { |
112
|
|
|
return @rename($temp_file_name, $dest_file_name); |
113
|
|
|
} |
114
|
|
|
@unlink($temp_file_name); |
|
|
|
|
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Delete data from cache |
120
|
|
|
* |
121
|
|
|
* @access public |
122
|
|
|
* |
123
|
|
|
* @param string $key |
124
|
|
|
* |
125
|
|
|
* @return bool true if the data was removed successfully |
126
|
|
|
*/ |
127
|
|
|
public function delete($key) |
128
|
|
|
{ |
129
|
|
|
$done = true; |
130
|
|
|
$ruta = $this->getRoute($key); |
131
|
|
|
if (file_exists($ruta)) { |
132
|
|
|
$done = @unlink($ruta); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $done; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Flush all cache |
140
|
|
|
* |
141
|
|
|
* @access public |
142
|
|
|
* @return bool always true |
143
|
|
|
*/ |
144
|
|
|
public function flush() |
145
|
|
|
{ |
146
|
|
|
foreach (scandir(getcwd() . '/' . self::$config['cache_path']) as $file_name) { |
147
|
|
|
if (file_exists(self::$config['cache_path'] . '/' . $file_name) && substr($file_name, -4) == '.php') { |
148
|
|
|
@unlink(self::$config['cache_path'] . '/' . $file_name); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: