|
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\SingletonTrait; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Cache |
|
11
|
|
|
* @package PSFS\base |
|
12
|
|
|
* Gestión de los ficheros de cache |
|
13
|
|
|
*/ |
|
14
|
|
|
class Cache |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var \Memcache |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $memcache = null; |
|
20
|
|
|
|
|
21
|
|
|
const JSON = 1; |
|
22
|
|
|
const TEXT = 2; |
|
23
|
|
|
const GZIP = 3; |
|
24
|
|
|
const JSONGZ = 4; |
|
25
|
|
|
const MEMCACHE = 5; |
|
26
|
|
|
|
|
27
|
|
|
use SingletonTrait; |
|
28
|
|
|
|
|
29
|
1 |
|
public function init() { |
|
30
|
1 |
|
if(Cache::canUseMemcache()) { |
|
31
|
|
|
$this->memcache = new \Memcached(); |
|
|
|
|
|
|
32
|
|
|
$this->memcache->connect(Config::getParam('memcache.host', '127.0.0.1'), Config::getParam('memcache.port', 11211)); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
1 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
5 |
|
public static function canUseMemcache() |
|
40
|
|
|
{ |
|
41
|
5 |
|
return Config::getParam('psfs.memcache', false) && !Config::getParam('debug') && class_exists('Memcached'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Método que guarda un text en un fichero |
|
46
|
|
|
* @param string $data |
|
47
|
|
|
* @param string $path |
|
48
|
|
|
* @throws ConfigException |
|
49
|
|
|
*/ |
|
50
|
4 |
View Code Duplication |
private function saveTextToFile($data, $path) |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
4 |
|
GeneratorHelper::createDir(dirname($path)); |
|
53
|
4 |
|
if (false === file_put_contents($path, $data)) { |
|
54
|
|
|
throw new ConfigException(_('No se tienen los permisos suficientes para escribir en el fichero ') |
|
55
|
|
|
. $path); |
|
56
|
|
|
} |
|
57
|
4 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Método que extrae el texto de un fichero |
|
61
|
|
|
* @param string $path |
|
62
|
|
|
* @param int $transform |
|
63
|
|
|
* @param boolean $absolute |
|
64
|
|
|
* @return mixed |
|
|
|
|
|
|
65
|
|
|
*/ |
|
66
|
5 |
|
public function getDataFromFile($path, $transform = Cache::TEXT, $absolute = false) |
|
67
|
|
|
{ |
|
68
|
5 |
|
$data = null; |
|
69
|
5 |
|
$absolutePath = ($absolute) ? $path : CACHE_DIR . DIRECTORY_SEPARATOR . $path; |
|
70
|
5 |
|
if(Cache::MEMCACHE && Cache::canUseMemcache()) { |
|
71
|
|
|
$data = $this->memcache->get(sha1($absolutePath)); |
|
72
|
5 |
|
} elseif (file_exists($absolutePath)) { |
|
73
|
3 |
|
$data = file_get_contents($absolutePath); |
|
74
|
|
|
} |
|
75
|
5 |
|
return Cache::extractDataWithFormat($data, $transform); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Método que verifica si un fichero tiene la cache expirada |
|
80
|
|
|
* @param string $path |
|
81
|
|
|
* @param int $expires |
|
82
|
|
|
* @param boolean $absolute |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
1 |
|
private function hasExpiredCache($path, $expires = 300, $absolute = false) |
|
86
|
|
|
{ |
|
87
|
1 |
|
$absolutePath = ($absolute) ? $path : CACHE_DIR . DIRECTORY_SEPARATOR . $path; |
|
88
|
1 |
|
$lasModificationDate = filemtime($absolutePath); |
|
89
|
1 |
|
return ($lasModificationDate + $expires <= time()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Método que transforma los datos de salida |
|
94
|
|
|
* @param string $data |
|
95
|
|
|
* @param int $transform |
|
96
|
|
|
* @return array|string|null |
|
97
|
|
|
*/ |
|
98
|
5 |
|
public static function extractDataWithFormat($data, $transform = Cache::TEXT) |
|
99
|
|
|
{ |
|
100
|
|
|
switch ($transform) { |
|
101
|
5 |
|
case Cache::JSON: |
|
102
|
5 |
|
$data = json_decode($data, true); |
|
103
|
5 |
|
break; |
|
104
|
3 |
|
case Cache::JSONGZ: |
|
105
|
3 |
|
$data = Cache::extractDataWithFormat($data, Cache::GZIP); |
|
106
|
3 |
|
$data = Cache::extractDataWithFormat($data, Cache::JSON); |
|
107
|
3 |
|
break; |
|
108
|
3 |
|
case Cache::GZIP: |
|
109
|
3 |
|
if (function_exists('gzuncompress') && !empty($data)) { |
|
110
|
1 |
|
$data = @gzuncompress($data ?: ''); |
|
111
|
|
|
} |
|
112
|
3 |
|
break; |
|
113
|
1 |
|
case Cache::MEMCACHE: |
|
114
|
|
|
$data = unserialize($data); |
|
115
|
|
|
} |
|
116
|
5 |
|
return $data; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Método que transforma los datos de entrada del fichero |
|
121
|
|
|
* @param string $data |
|
122
|
|
|
* @param int $transform |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
4 |
|
public static function transformData($data, $transform = Cache::TEXT) |
|
126
|
|
|
{ |
|
127
|
|
|
switch ($transform) { |
|
128
|
4 |
|
case Cache::JSON: |
|
129
|
4 |
|
$data = json_encode($data, JSON_PRETTY_PRINT); |
|
130
|
4 |
|
break; |
|
131
|
2 |
|
case Cache::JSONGZ: |
|
132
|
1 |
|
$data = Cache::transformData($data, Cache::JSON); |
|
133
|
1 |
|
$data = Cache::transformData($data, Cache::GZIP); |
|
134
|
1 |
|
break; |
|
135
|
2 |
|
case Cache::GZIP: |
|
136
|
1 |
|
if (function_exists('gzcompress')) { |
|
137
|
1 |
|
$data = gzcompress($data ?: ''); |
|
138
|
|
|
} |
|
139
|
1 |
|
break; |
|
140
|
2 |
|
case Cache::MEMCACHE: |
|
141
|
|
|
$data = serialize($data); |
|
142
|
|
|
break; |
|
143
|
|
|
} |
|
144
|
4 |
|
return $data; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Método que guarda en fichero los datos pasados |
|
149
|
|
|
* @param $path |
|
150
|
|
|
* @param $data |
|
151
|
|
|
* @param int $transform |
|
152
|
|
|
* @param boolean $absolute |
|
153
|
|
|
* @param integer $expires |
|
154
|
|
|
*/ |
|
155
|
4 |
|
public function storeData($path, $data, $transform = Cache::TEXT, $absolute = false, $expires = 600) |
|
156
|
|
|
{ |
|
157
|
4 |
|
$data = Cache::transformData($data, $transform); |
|
158
|
4 |
|
$absolutePath = ($absolute) ? $path : CACHE_DIR . DIRECTORY_SEPARATOR . $path; |
|
159
|
4 |
|
if(Cache::MEMCACHE == $transform) { |
|
160
|
|
|
$this->memcache->set(sha1($absolutePath), $data, $expires); |
|
161
|
|
|
} else { |
|
162
|
4 |
|
$this->saveTextToFile($data, $absolutePath); |
|
163
|
|
|
} |
|
164
|
4 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Método que verifica si tiene que leer o no un fichero de cache |
|
168
|
|
|
* @param string $path |
|
169
|
|
|
* @param int $expires |
|
170
|
|
|
* @param callable $function |
|
171
|
|
|
* @param int $transform |
|
172
|
|
|
* @return mixed |
|
173
|
|
|
*/ |
|
174
|
1 |
|
public function readFromCache($path, $expires = 300, callable $function, $transform = Cache::TEXT) |
|
175
|
|
|
{ |
|
176
|
1 |
|
$data = null; |
|
177
|
1 |
|
if (file_exists(CACHE_DIR . DIRECTORY_SEPARATOR . $path)) { |
|
178
|
1 |
|
if (null !== $function && $this->hasExpiredCache($path, $expires)) { |
|
179
|
1 |
|
$data = call_user_func($function); |
|
180
|
1 |
|
$this->storeData($path, $data, $transform, false, $expires); |
|
181
|
|
|
} else { |
|
182
|
1 |
|
$data = $this->getDataFromFile($path, $transform); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
1 |
|
return $data; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return bool |
|
190
|
|
|
*/ |
|
191
|
1 |
|
private static function checkAdminSite() |
|
192
|
|
|
{ |
|
193
|
1 |
|
$isAdminRequest = false; |
|
194
|
1 |
|
$lastRequest = Security::getInstance()->getSessionKey("lastRequest"); |
|
195
|
1 |
|
if (null !== $lastRequest) { |
|
196
|
|
|
$url = str_replace(Request::getInstance()->getRootUrl(true), '', $lastRequest['url']); |
|
197
|
|
|
$isAdminRequest = preg_match('/^\/admin\//i', $url); |
|
198
|
|
|
} |
|
199
|
1 |
|
return (bool)$isAdminRequest; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Método estático que revisa si se necesita cachear la respuesta de un servicio o no |
|
204
|
|
|
* @return integer|boolean |
|
205
|
|
|
*/ |
|
206
|
1 |
|
public static function needCache() |
|
207
|
|
|
{ |
|
208
|
1 |
|
$needCache = false; |
|
209
|
1 |
|
if (!self::checkAdminSite()) { |
|
210
|
1 |
|
$action = Security::getInstance()->getSessionKey("__CACHE__"); |
|
211
|
1 |
|
if (null !== $action && array_key_exists("cache", $action) && $action["cache"] > 0) { |
|
212
|
1 |
|
$needCache = $action["cache"]; |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
1 |
|
return $needCache; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Método que construye un hash para almacenar la cache |
|
220
|
|
|
* @return string |
|
221
|
|
|
*/ |
|
222
|
1 |
|
public function getRequestCacheHash() |
|
223
|
|
|
{ |
|
224
|
1 |
|
$hash = ""; |
|
225
|
1 |
|
$action = Security::getInstance()->getSessionKey("__CACHE__"); |
|
226
|
1 |
|
if (null !== $action && $action["cache"] > 0) { |
|
227
|
1 |
|
$hash = $action["http"] . " " . $action["slug"]; |
|
228
|
|
|
} |
|
229
|
1 |
|
return sha1($hash); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..