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