Skip to content

Instantly share code, notes, and snippets.

@sukhikh18
Last active June 25, 2021 07:36
Show Gist options
  • Save sukhikh18/d1d6ba1d484ef7c767323db3517eea52 to your computer and use it in GitHub Desktop.
Save sukhikh18/d1d6ba1d484ef7c767323db3517eea52 to your computer and use it in GitHub Desktop.
Кэширования данных (D7) #Bitrix
<?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