Last active
June 25, 2021 07:36
-
-
Save sukhikh18/d1d6ba1d484ef7c767323db3517eea52 to your computer and use it in GitHub Desktop.
Кэширования данных (D7) #Bitrix
This file contains 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
<?php | |
use Bitrix\Main; | |
use Bitrix\Iblock; | |
function getNews() | |
{ | |
$arNews = []; | |
$arQueryParams = [ | |
'select' => ['*'], | |
'filter' => ['IBLOCK.CODE' => 'news'], | |
'order' => ['SORT' => 'ASC', 'DATE_CREATE' => 'DESC'], | |
]; | |
$obCache = Main\Data\Cache::createInstance(); | |
if ($obCache->initCache($cache_ttl = 86400, $cache_id = serialize($arQueryParams), 'Example')) | |
{ | |
$arNews = $obCache->GetVars(); | |
} | |
elseif ($obCache->startDataCache()) | |
{ | |
try { | |
$rsResult = Iblock\ElementTable::getList($arQueryParams); | |
if (! $rsResult->getSelectedRowsCount()) { | |
throw new \Exception('К сожалению, ничего не найдено'); | |
} | |
$arNews = $rsResult->fetchAll(); | |
$obCache->endDataCache($arNews); | |
} | |
catch (\Exception|Main\ArgumentException $e) | |
{ | |
$obCache->abortDataCache(); | |
} | |
} | |
return $arNews; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment