|
1
|
|
|
<?php |
|
2
|
|
|
namespace PSFS\base; |
|
3
|
|
|
|
|
4
|
|
|
use PSFS\base\config\Config; |
|
5
|
|
|
use PSFS\base\exception\ConfigException; |
|
6
|
|
|
use PSFS\base\types\helpers\GeneratorHelper; |
|
7
|
|
|
use PSFS\base\types\helpers\FileHelper; |
|
8
|
|
|
use PSFS\base\types\traits\SingletonTrait; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Cache |
|
12
|
|
|
* @package PSFS\base |
|
13
|
|
|
* Gestión de los ficheros de cache |
|
14
|
|
|
*/ |
|
15
|
|
|
class Cache |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \Memcache |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $memcache = null; |
|
21
|
|
|
|
|
22
|
|
|
const JSON = 1; |
|
23
|
|
|
const TEXT = 2; |
|
24
|
|
|
const GZIP = 3; |
|
25
|
|
|
const JSONGZ = 4; |
|
26
|
|
|
const MEMCACHE = 5; |
|
27
|
|
|
|
|
28
|
|
|
use SingletonTrait; |
|
29
|
|
|
|
|
30
|
2 |
|
public function __construct() |
|
31
|
|
|
{ |
|
32
|
2 |
|
$this->setLoaded(true); |
|
33
|
2 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @return bool |
|
37
|
|
|
*/ |
|
38
|
3 |
|
public static function canUseMemcache() |
|
39
|
|
|
{ |
|
40
|
3 |
|
return Config::getParam('psfs.memcache', false) && !Config::getParam('debug') && class_exists('Memcached'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Método que guarda un text en un fichero |
|
45
|
|
|
* @param string $data |
|
46
|
|
|
* @param string $path |
|
47
|
|
|
* @throws ConfigException |
|
48
|
|
|
*/ |
|
49
|
6 |
View Code Duplication |
private function saveTextToFile($data, $path) |
|
50
|
|
|
{ |
|
51
|
6 |
|
GeneratorHelper::createDir(dirname($path)); |
|
52
|
6 |
|
if (false === FileHelper::writeFile($path, $data)) { |
|
53
|
|
|
throw new ConfigException(_('No se tienen los permisos suficientes para escribir en el fichero ') . $path); |
|
54
|
|
|
} |
|
55
|
6 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Método que extrae el texto de un fichero |
|
59
|
|
|
* @param string $path |
|
60
|
|
|
* @param int $transform |
|
61
|
|
|
* @param boolean $absolute |
|
62
|
|
|
* @return mixed |
|
|
|
|
|
|
63
|
|
|
*/ |
|
64
|
8 |
|
public function getDataFromFile($path, $transform = Cache::TEXT, $absolute = false) |
|
65
|
|
|
{ |
|
66
|
8 |
|
Logger::log('Gathering data from cache', LOG_DEBUG, ['path' => $path]); |
|
67
|
8 |
|
$data = null; |
|
68
|
8 |
|
$absolutePath = ($absolute) ? $path : CACHE_DIR . DIRECTORY_SEPARATOR . $path; |
|
69
|
8 |
|
if (file_exists($absolutePath)) { |
|
70
|
5 |
|
$data = FileHelper::readFile($absolutePath); |
|
71
|
|
|
} |
|
72
|
8 |
|
return Cache::extractDataWithFormat($data, $transform); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Método que verifica si un fichero tiene la cache expirada |
|
77
|
|
|
* @param string $path |
|
78
|
|
|
* @param int $expires |
|
79
|
|
|
* @param boolean $absolute |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
1 |
|
private function hasExpiredCache($path, $expires = 300, $absolute = false) |
|
83
|
|
|
{ |
|
84
|
1 |
|
Logger::log('Checking expiration', LOG_DEBUG, ['path' => $path]); |
|
85
|
1 |
|
$absolutePath = ($absolute) ? $path : CACHE_DIR . DIRECTORY_SEPARATOR . $path; |
|
86
|
1 |
|
$lasModificationDate = filemtime($absolutePath); |
|
87
|
1 |
|
return ($lasModificationDate + $expires <= time()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Método que transforma los datos de salida |
|
92
|
|
|
* @param string $data |
|
93
|
|
|
* @param int $transform |
|
94
|
|
|
* @return array|string|null |
|
95
|
|
|
*/ |
|
96
|
8 |
View Code Duplication |
public static function extractDataWithFormat($data, $transform = Cache::TEXT) |
|
97
|
|
|
{ |
|
98
|
8 |
|
Logger::log('Extracting data from cache', LOG_DEBUG); |
|
99
|
|
|
switch ($transform) { |
|
100
|
8 |
|
case Cache::JSON: |
|
101
|
8 |
|
$data = json_decode($data, true); |
|
102
|
8 |
|
break; |
|
103
|
5 |
|
case Cache::JSONGZ: |
|
104
|
5 |
|
$data = Cache::extractDataWithFormat($data, Cache::GZIP); |
|
105
|
5 |
|
$data = Cache::extractDataWithFormat($data, Cache::JSON); |
|
|
|
|
|
|
106
|
5 |
|
break; |
|
107
|
5 |
|
case Cache::GZIP: |
|
108
|
5 |
|
if (function_exists('gzuncompress') && !empty($data)) { |
|
109
|
2 |
|
$data = @gzuncompress($data ?: ''); |
|
110
|
|
|
} |
|
111
|
5 |
|
break; |
|
112
|
|
|
} |
|
113
|
8 |
|
return $data; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Método que transforma los datos de entrada del fichero |
|
118
|
|
|
* @param string $data |
|
119
|
|
|
* @param int $transform |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
6 |
View Code Duplication |
public static function transformData($data, $transform = Cache::TEXT) |
|
123
|
|
|
{ |
|
124
|
6 |
|
Logger::log('Transform data in cache', LOG_DEBUG); |
|
125
|
|
|
switch ($transform) { |
|
126
|
6 |
|
case Cache::JSON: |
|
127
|
6 |
|
$data = json_encode($data, JSON_PRETTY_PRINT); |
|
128
|
6 |
|
break; |
|
129
|
3 |
|
case Cache::JSONGZ: |
|
130
|
2 |
|
$data = Cache::transformData($data, Cache::JSON); |
|
131
|
2 |
|
$data = Cache::transformData($data, Cache::GZIP); |
|
132
|
2 |
|
break; |
|
133
|
3 |
|
case Cache::GZIP: |
|
134
|
2 |
|
if (function_exists('gzcompress')) { |
|
135
|
2 |
|
$data = gzcompress($data ?: ''); |
|
136
|
|
|
} |
|
137
|
2 |
|
break; |
|
138
|
|
|
} |
|
139
|
6 |
|
return $data; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Método que guarda en fichero los datos pasados |
|
144
|
|
|
* @param $path |
|
145
|
|
|
* @param $data |
|
146
|
|
|
* @param int $transform |
|
147
|
|
|
* @param boolean $absolute |
|
148
|
|
|
* @param integer $expires |
|
149
|
|
|
*/ |
|
150
|
6 |
|
public function storeData($path, $data, $transform = Cache::TEXT, $absolute = false, $expires = 600) |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
6 |
|
Logger::log('Store data in cache', LOG_DEBUG, ['path' => $path]); |
|
153
|
6 |
|
$data = Cache::transformData($data, $transform); |
|
154
|
6 |
|
$absolutePath = ($absolute) ? $path : CACHE_DIR . DIRECTORY_SEPARATOR . $path; |
|
155
|
6 |
|
$this->saveTextToFile($data, $absolutePath); |
|
156
|
6 |
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Método que verifica si tiene que leer o no un fichero de cache |
|
160
|
|
|
* @param string $path |
|
161
|
|
|
* @param int $expires |
|
162
|
|
|
* @param callable $function |
|
|
|
|
|
|
163
|
|
|
* @param int $transform |
|
164
|
|
|
* @return mixed |
|
165
|
|
|
*/ |
|
166
|
1 |
|
public function readFromCache($path, $expires = 300, $function = null, $transform = Cache::TEXT) |
|
167
|
|
|
{ |
|
168
|
1 |
|
$data = null; |
|
169
|
1 |
|
Logger::log('Reading data from cache', LOG_DEBUG, ['path' => $path]); |
|
170
|
1 |
|
if (file_exists(CACHE_DIR . DIRECTORY_SEPARATOR . $path)) { |
|
171
|
1 |
|
if (is_callable($function) && $this->hasExpiredCache($path, $expires)) { |
|
172
|
1 |
|
$data = call_user_func($function); |
|
173
|
1 |
|
$this->storeData($path, $data, $transform, false, $expires); |
|
174
|
|
|
} else { |
|
175
|
1 |
|
$data = $this->getDataFromFile($path, $transform); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
1 |
|
return $data; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return bool |
|
183
|
|
|
*/ |
|
184
|
1 |
|
private static function checkAdminSite() |
|
185
|
|
|
{ |
|
186
|
1 |
|
return Security::getInstance()->canAccessRestrictedAdmin(); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Método estático que revisa si se necesita cachear la respuesta de un servicio o no |
|
191
|
|
|
* @return integer|boolean |
|
192
|
|
|
*/ |
|
193
|
1 |
|
public static function needCache() |
|
194
|
|
|
{ |
|
195
|
1 |
|
$needCache = false; |
|
196
|
1 |
|
Logger::log('Checking cache requirements', LOG_DEBUG); |
|
197
|
1 |
|
if (!self::checkAdminSite() && !Config::getParam('debug') && Config::getParam('cache.data.enable', false)) { |
|
198
|
1 |
|
$action = Security::getInstance()->getSessionKey("__CACHE__"); |
|
199
|
1 |
|
Logger::log('Gathering cache params from Session', LOG_DEBUG, $action); |
|
200
|
1 |
|
if (null !== $action && array_key_exists("cache", $action) && $action["cache"] > 0) { |
|
201
|
1 |
|
$needCache = $action["cache"]; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
1 |
|
return $needCache; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Método que construye un hash para almacenar la cache |
|
209
|
|
|
* @return array |
|
|
|
|
|
|
210
|
|
|
*/ |
|
211
|
1 |
|
public function getRequestCacheHash() |
|
212
|
|
|
{ |
|
213
|
1 |
|
$hashPath = null; |
|
214
|
1 |
|
$filename = null; |
|
215
|
1 |
|
$action = Security::getInstance()->getSessionKey("__CACHE__"); |
|
216
|
1 |
|
Logger::log('Gathering cache hash for request', LOG_DEBUG, $action); |
|
217
|
1 |
|
if (null !== $action && $action["cache"] > 0) { |
|
218
|
1 |
|
$query = $action['params']; |
|
219
|
1 |
|
$query['X-API-LANG'] = Request::header('X-API-LANG', 'es'); |
|
220
|
1 |
|
$filename = FileHelper::generateHashFilename($action["http"], $action["slug"], $query); |
|
221
|
1 |
|
$hashPath = FileHelper::generateCachePath($action, $query); |
|
222
|
1 |
|
Logger::log('Cache file calculated', LOG_DEBUG, ['file' => $filename, 'hash' => $hashPath]); |
|
223
|
|
|
} |
|
224
|
1 |
|
return [$hashPath, $filename]; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Flush cache when save a registry |
|
229
|
|
|
*/ |
|
230
|
1 |
|
public function flushCache() { |
|
231
|
1 |
|
if(Config::getParam('cache.data.enable', false)) { |
|
232
|
1 |
|
Logger::log('Flushing cache', LOG_DEBUG); |
|
233
|
1 |
|
$action = Security::getInstance()->getSessionKey("__CACHE__"); |
|
234
|
1 |
|
$hashPath = FileHelper::generateCachePath($action, $action['params']) . '..' . DIRECTORY_SEPARATOR . ' .. ' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR; |
|
|
|
|
|
|
235
|
1 |
|
if(!file_exists($hashPath)) { |
|
236
|
1 |
|
$hashPath = CACHE_DIR . DIRECTORY_SEPARATOR . $hashPath; |
|
237
|
|
|
} |
|
238
|
1 |
|
FileHelper::deleteDir($hashPath); |
|
239
|
|
|
} |
|
240
|
1 |
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.