1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
cache.php
См. документацию.
1<?php
2
9
10namespace Bitrix\Main\Data;
11
12use Bitrix\Main;
13use Bitrix\Main\Config;
14use Bitrix\Main\Diag;
15
16class Cache
17{
19
20 protected $content;
21 protected $vars;
22 protected int $ttl = 0;
23 protected string $uniqueString = '';
24 protected string $baseDir = 'cache';
25 protected string $initDir = '';
26 protected string $filename = '';
27 protected bool $isStarted = false;
28
29 protected static $showCacheStat = false;
30 protected static $clearCache = null;
31 protected static $clearCacheSession = null;
32
33 protected bool $forceRewriting = false;
34 protected bool $hasOutput = true;
35
36 public function __construct($cacheEngine)
37 {
38 $this->cacheEngine = $cacheEngine;
39 }
40
41 public static function createCacheEngine($params = [])
42 {
43 static $instances = [];
44
45 $hash = sha1(serialize($params));
46 if (isset($instances[$hash]))
47 {
48 return clone $instances[$hash];
49 }
50
51 $cacheEngine = null;
52
53 // Events can't be used here because events use cache
54 $cacheType = 'files';
56 if (!empty($v['type']))
57 {
58 $cacheType = $v['type'];
59 }
60
61 if (is_array($cacheType))
62 {
63 if (isset($cacheType['class_name']))
64 {
65 if (!isset($cacheType['extension']) || extension_loaded($cacheType['extension']))
66 {
67 if (isset($cacheType['required_file']) && ($requiredFile = Main\Loader::getLocal($cacheType['required_file'])) !== false)
68 {
69 require_once($requiredFile);
70 }
71
72 if (isset($cacheType['required_remote_file']))
73 {
74 require_once($cacheType['required_remote_file']);
75 }
76
77 $className = $cacheType['class_name'];
78 if (class_exists($className))
79 {
80 $cacheEngine = new $className($params);
81 }
82 }
83 }
84 }
85 else
86 {
87 switch ($cacheType)
88 {
89 case 'redis':
91 break;
92 case 'memcached':
94 break;
95 case 'memcache':
97 break;
98 case 'apc':
99 case 'apcu':
101 break;
102 case 'files':
104 break;
105 default:
107 break;
108 }
109 }
110
111 if ($cacheEngine === null)
112 {
114 trigger_error('Cache engine is not found', E_USER_WARNING);
115 }
116
117 if (!$cacheEngine->isAvailable())
118 {
120 trigger_error('Cache engine is not available', E_USER_WARNING);
121 }
122
123 $instances[$hash] = $cacheEngine;
124
125 return $cacheEngine;
126 }
127
128 public static function getCacheEngineType()
129 {
130 $obj = static::createCacheEngine();
131 $class = get_class($obj);
132 if (($pos = strrpos($class, "\\")) !== false)
133 {
134 $class = substr($class, $pos + 1);
135 }
136
137 return strtolower($class);
138 }
139
144 public static function createInstance($params = [])
145 {
146 $cacheEngine = static::createCacheEngine($params);
147 return new static($cacheEngine);
148 }
149
150 public static function setShowCacheStat($showCacheStat)
151 {
152 static::$showCacheStat = $showCacheStat;
153 }
154
155 public static function getShowCacheStat()
156 {
157 return static::$showCacheStat;
158 }
159
164 public static function setClearCache($clearCache)
165 {
166 static::$clearCache = $clearCache;
167 }
168
174 {
175 static::$clearCacheSession = $clearCacheSession;
176 }
177
178 public static function getSalt()
179 {
180 $context = Main\Application::getInstance()->getContext();
181 $server = $context->getServer();
182
183 $scriptName = $server->get('SCRIPT_NAME');
184 if ($scriptName == '/bitrix/urlrewrite.php' && (($v = $server->get('REAL_FILE_PATH')) != null))
185 {
186 $scriptName = $v;
187 }
188 elseif ($scriptName == '/404.php' && (($v = $server->get('REAL_FILE_PATH')) != null))
189 {
190 $scriptName = $v;
191 }
192 return '/' . mb_substr(md5($scriptName), 0, 3);
193 }
194
199 public static function shouldClearCache()
200 {
201 global $USER;
202
204
205 if (!$application->isInitialized())
206 {
207 return false;
208 }
209
210 $kernelSession = $application->getKernelSession();
211
212 if (isset(static::$clearCacheSession) || isset(static::$clearCache))
213 {
214 if ($USER instanceof \CUser && $USER->CanDoOperation('cache_control'))
215 {
216 if (isset(static::$clearCacheSession))
217 {
218 if (static::$clearCacheSession === true)
219 {
220 $kernelSession['SESS_CLEAR_CACHE'] = 'Y';
221 }
222 else
223 {
224 unset($kernelSession['SESS_CLEAR_CACHE']);
225 }
226 }
227
228 if (isset(static::$clearCache) && (static::$clearCache === true))
229 {
230 return true;
231 }
232 }
233 }
234
235 if (isset($kernelSession['SESS_CLEAR_CACHE']) && $kernelSession['SESS_CLEAR_CACHE'] === 'Y')
236 {
237 return true;
238 }
239
240 return false;
241 }
242
243 public static function getPath($uniqueString)
244 {
245 $un = md5($uniqueString);
246 return mb_substr($un, 0, 2) . '/' . $un . '.php';
247 }
248
249 public function clean($uniqueString, $initDir = false, $baseDir = 'cache')
250 {
251 $personalRoot = Main\Application::getPersonalRoot();
252 $baseDir = $personalRoot . '/' . $baseDir . '/';
254
255 if (static::$showCacheStat)
256 {
258 }
259
260 return $this->cacheEngine->clean($baseDir, $initDir, '/' . $filename);
261 }
262
263 public function cleanDir($initDir = false, $baseDir = 'cache')
264 {
265 $personalRoot = Main\Application::getPersonalRoot();
266 $baseDir = $personalRoot . '/' . $baseDir . '/';
267
268 if (static::$showCacheStat)
269 {
270 Diag\CacheTracker::add(0, "", $baseDir, $initDir, "", "C");
271 }
272
273 $this->cacheEngine->clean($baseDir, $initDir);
274 }
275
276 public function initCache($ttl, $uniqueString, $initDir = false, $baseDir = 'cache')
277 {
278 if ($initDir === false)
279 {
280 $initDir = 'default';
281 }
282
283 $personalRoot = Main\Application::getPersonalRoot();
284 $this->baseDir = $personalRoot . '/' . $baseDir . '/';
285 $this->initDir = $initDir;
286 $this->filename = '/' . $this->getPath($uniqueString);
287 $this->ttl = $ttl;
288 $this->uniqueString = $uniqueString;
289 $this->vars = false;
290
291 if ($ttl <= 0 || $this->forceRewriting || static::shouldClearCache())
292 {
293 return false;
294 }
295
296 $data = ['CONTENT' => '', 'VARS' => ''];
297 if (!$this->cacheEngine->read($data, $this->baseDir, $this->initDir, $this->filename, $this->ttl))
298 {
299 return false;
300 }
301
302 if (!is_array($data) || empty($data) || !isset($data['CONTENT']) || !isset($data['VARS']))
303 {
304 return false;
305 }
306
307 if (static::$showCacheStat)
308 {
309 $read = 0;
310 $path = '';
311 if ($this->cacheEngine instanceof CacheEngineStatInterface)
312 {
313 $read = $this->cacheEngine->getReadBytes();
314 $path = $this->cacheEngine->getCachePath();
315 }
316
318 Diag\CacheTracker::add($read, $path, $this->baseDir, $this->initDir, $this->filename, 'R');
319 }
320
321 $this->content = $data['CONTENT'];
322 $this->vars = $data['VARS'];
323
324 return true;
325 }
326
327 public function output()
328 {
329 if ($this->hasOutput)
330 {
331 echo $this->content;
332 }
333 }
334
335 public function noOutput()
336 {
337 $this->hasOutput = false;
338 }
339
340 public function getVars()
341 {
342 return $this->vars;
343 }
344
345 public function startDataCache($TTL = false, $uniqueString = false, $initDir = false, $vars = array(), $baseDir = 'cache')
346 {
347 $narg = func_num_args();
348 if ($narg <= 0)
349 {
350 $TTL = $this->ttl;
351 }
352
353 if ($narg <= 1)
354 {
355 $uniqueString = $this->uniqueString;
356 }
357
358 if ($narg <= 2)
359 {
360 $initDir = $this->initDir;
361 }
362
363 if ($narg <= 3)
364 {
365 $vars = $this->vars;
366 }
367
368 if ($this->initCache($TTL, $uniqueString, $initDir, $baseDir))
369 {
370 $this->output();
371 return false;
372 }
373
374 if ($TTL <= 0)
375 {
376 return true;
377 }
378
379 if ($this->hasOutput)
380 {
381 ob_start();
382 }
383
384 $this->vars = $vars;
385 $this->isStarted = true;
386
387 return true;
388 }
389
390 public function abortDataCache()
391 {
392 if (!$this->isStarted)
393 {
394 return;
395 }
396
397 $this->isStarted = false;
398 if ($this->hasOutput)
399 {
400 ob_end_flush();
401 }
402 }
403
404 public function endDataCache($vars = false)
405 {
406 if (!$this->isStarted)
407 {
408 return;
409 }
410
411 $this->isStarted = false;
412 $data = [
413 'CONTENT' => $this->hasOutput ? ob_get_contents() : '',
414 'VARS' => ($vars !== false ? $vars : $this->vars),
415 ];
416
417 $this->cacheEngine->write($data, $this->baseDir, $this->initDir, $this->filename, $this->ttl);
418
419 if (static::$showCacheStat)
420 {
421 $written = 0;
422 $path = '';
423 if ($this->cacheEngine instanceof CacheEngineStatInterface)
424 {
425 $written = $this->cacheEngine->getWrittenBytes();
426 $path = $this->cacheEngine->getCachePath();
427 }
428
430 Diag\CacheTracker::add($written, $path, $this->baseDir, $this->initDir, $this->filename, 'W');
431 }
432
433 if ($this->hasOutput)
434 {
435 if (ob_get_contents() <> '')
436 {
437 ob_end_flush();
438 }
439 else
440 {
441 ob_end_clean();
442 }
443 }
444 }
445
446 public function isCacheExpired($path)
447 {
448 return $this->cacheEngine->isCacheExpired($path);
449 }
450
451 public function isStarted()
452 {
453 return $this->isStarted;
454 }
455
461 public static function clearCache($full = false, $initDir = ''): void
462 {
463 if ($initDir === '' && is_string($full))
464 {
465 $initDir = $full;
466 $full = true;
467 }
468
469 if ($full === true)
470 {
471 $cache = static::createInstance();
472 $cache->cleanDir($initDir);
473 }
474 }
475
480 public function forceRewriting($mode)
481 {
482 $this->forceRewriting = (bool) $mode;
483 }
484}
$path
Определения access_edit.php:21
$hash
Определения ajax_redirector.php:8
static getInstance()
Определения application.php:98
static getPersonalRoot()
Определения application.php:762
static getValue($name)
Определения configuration.php:24
$vars
Определения cache.php:21
static $showCacheStat
Определения cache.php:29
int $ttl
Определения cache.php:22
isStarted()
Определения cache.php:451
forceRewriting($mode)
Определения cache.php:480
clean($uniqueString, $initDir=false, $baseDir='cache')
Определения cache.php:249
output()
Определения cache.php:327
endDataCache($vars=false)
Определения cache.php:404
$content
Определения cache.php:20
static clearCache($full=false, $initDir='')
Определения cache.php:461
isCacheExpired($path)
Определения cache.php:446
static createInstance($params=[])
Определения cache.php:144
bool $forceRewriting
Определения cache.php:33
static getCacheEngineType()
Определения cache.php:128
noOutput()
Определения cache.php:335
static setClearCacheSession($clearCacheSession)
Определения cache.php:173
static shouldClearCache()
Определения cache.php:199
static $clearCacheSession
Определения cache.php:31
static setClearCache($clearCache)
Определения cache.php:164
cleanDir($initDir=false, $baseDir='cache')
Определения cache.php:263
bool $hasOutput
Определения cache.php:34
getVars()
Определения cache.php:340
__construct($cacheEngine)
Определения cache.php:36
abortDataCache()
Определения cache.php:390
string $uniqueString
Определения cache.php:23
static getPath($uniqueString)
Определения cache.php:243
string $filename
Определения cache.php:26
CacheEngineInterface $cacheEngine
Определения cache.php:18
static setShowCacheStat($showCacheStat)
Определения cache.php:150
string $initDir
Определения cache.php:25
static $clearCache
Определения cache.php:30
static createCacheEngine($params=[])
Определения cache.php:41
startDataCache($TTL=false, $uniqueString=false, $initDir=false, $vars=array(), $baseDir='cache')
Определения cache.php:345
bool $isStarted
Определения cache.php:27
initCache($ttl, $uniqueString, $initDir=false, $baseDir='cache')
Определения cache.php:276
string $baseDir
Определения cache.php:24
static getSalt()
Определения cache.php:178
static getShowCacheStat()
Определения cache.php:155
static addCacheStatBytes($cacheStatBytes)
Определения cachetracker.php:33
static add($size, $path, $baseDir, $initDir, $filename, $operation)
Определения cachetracker.php:43
static getLocal($path, $root=null)
Определения loader.php:572
$content
Определения commerceml.php:144
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
global $USER
Определения csv_new_run.php:40
$context
Определения csv_new_setup.php:223
$application
Определения bitrix.php:23
$kernelSession
Определения include.php:181
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799