Last active
August 26, 2021 13:55
-
-
Save vozhukh/17ec0533dff4e241820f1920a6406b64 to your computer and use it in GitHub Desktop.
d7 кешування + теговий кеш
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
use \Bitrix\Main\Data\Cache; | |
//<ПРостий кеш> | |
$cache = Cache::createInstance(); // отрматике ексзепляр класу | |
if ($cache->initCache(7200, "cache_key")) { | |
$vars = $cache->getVars(); // отримани змінні з кешу | |
} | |
elseif ($cache->startDataCache()) { | |
// некоторые действия... | |
$cache->endDataCache(array("key" => "value")); // запиш в кеш | |
//</ПРостий кеш> | |
//<керований кеш> | |
$cache = \Bitrix\Main\Application::getInstance()->getManagedCache(); | |
if ($cache->read($cacheTtl, $cacheId)) { | |
$vars = $cache->get($cacheId); // достаем переменные из кеша | |
} else { | |
// операції для оримання $value... | |
$cache_manager = Bitrix\Main\Application::getInstance()->getTaggedCache(); | |
$cache_manager->startTagCache('/path_cache'); | |
$cache_manager->registerTag('tag'); | |
$cache_manager->endTagCache(); | |
$cache->set($cacheId, array("key" => $value)); // записываем в кеш | |
} | |
//Для примусового очищення кеша по ключу використовуємо: | |
$cache->clean($cacheId); | |
//</керований кеш> | |
//<Добавленя тегу> | |
$cache_manager = Bitrix\Main\Application::getInstance()->getTaggedCache(); | |
$cache_manager->startTagCache('/path_cache'); | |
$cache_manager->registerTag('tag'); | |
$cache_manager->endTagCache(); | |
// <Приклад 2 тегровий кеш> | |
$cache = \Bitrix\Main\Application::getInstance()->getManagedCache(); | |
$cacheId = md5('wv_'.serialize($arResult['VARIABLES'])); | |
$cacheTtl = ((int)$arParams["CACHE_TIME"])?$arParams["CACHE_TIME"]:3600000; | |
$cachePath = '/element_id_code' | |
if ($cache->read($cacheTtl, $cacheId)) { | |
$ItemID = $cache->get($cacheId); // достаем переменные из кеша | |
} else { | |
$ItemID = \Local\Iblocks\Element::getPrductIdbyCode(array( | |
'PRODUCT_CODE'=>$arResult['VARIABLES']['ELEMENT_CODE'], | |
'IBLOCK_ID'=>$arParams["IBLOCK_ID"], | |
'SECTION_CODE'=>$arResult['VARIABLES']['SECTION_CODE'], | |
)); | |
$cache_manager = Bitrix\Main\Application::getInstance()->getTaggedCache(); | |
$cache_manager->startTagCache($cachePath); | |
$cache_manager->registerTag('iblock_id_'.$arParams["IBLOCK_ID"]); | |
$cache_manager->endTagCache(); | |
$cache->set($cacheId, intval($ItemID)); // записываем в кеш | |
} | |
// </Приклад 2 тегровий кеш> | |
//Почистити по тегові | |
$cache_manager->clearByTag('tag'); | |
//</Добавленя тегу> | |
// | |
/** | |
* @access public | |
* @param mixed $TTL �час життя в секундах | |
* @param mixed $uniqueString | |
* @param bool $initDir (default: false) каталог де буде збегірігатись віднсно - /bitrix/cache/ | |
* @param string $baseDir (default: "cache") базовий каталог кешу. По замовчувані cache | |
* @return bool true, якщо кеш дійсний | |
*/ | |
public function initCache($TTL, $uniqueString, $initDir = false, $baseDir = "cache") | |
public static function clean($basedir, $initdir = false, $filename = false); | |
public static function read(&$arAllVars, $basedir, $initdir, $filename, $TTL); | |
public static function write($arAllVars, $basedir, $initdir, $filename, $TTL); | |
public static function IsCacheExpired($path); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment