- Регистрируем через автолоад (composer или битрикс) обьект IblockCacheAgent
- Через консоль
/bitrix/admin/php_command_line.php?lang=ru
включаем агент\Thelh\Utils\IblockCacheAgent::registerAgent();
- Готово
Last active
April 29, 2022 12:19
-
-
Save Isa3v/ddf42a1fc8914ba0b014e5297d89f3b8 to your computer and use it in GitHub Desktop.
Сбрасываем кеш инфоблоков, если наступило время активации элемента
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 | |
namespace Thelh\Utils; | |
class IblockCacheAgent | |
{ | |
public static function agent($lastTimesteampOnRunAgent = 0) | |
{ | |
\Bitrix\Main\Loader::includeModule('iblock'); | |
// Получаем последнюю дату запуска агента и делаем от него отсчет | |
$currentDateTime = new \Bitrix\Main\Type\DateTime(); | |
$timestampCheckInt = 1000000000; | |
$lastTimesteampOnRunAgent = (int) $lastTimesteampOnRunAgent; | |
$lastTimesteampOnRunAgent = ($lastTimesteampOnRunAgent > $timestampCheckInt) ? $lastTimesteampOnRunAgent : $currentDateTime->getTimeStamp(); | |
$fromDate = \Bitrix\Main\Type\DateTime::createFromTimestamp($lastTimesteampOnRunAgent); | |
// Ищем элементы активированные за период | |
$iblockElements = \Bitrix\Iblock\ElementTable::query() | |
->where('ACTIVE', true) | |
->where('ACTIVE_FROM', '>=', $fromDate) | |
->where('ACTIVE_FROM', '<=', $currentDateTime) | |
->addSelect('ID') | |
->addSelect('IBLOCK_ID') | |
->fetchCollection(); | |
if ($iblockElements && $iblockElements->count() > 0) { | |
// Проходимся и удаляем тегированный кеш | |
\CIBlock::enableClearTagCache(); | |
foreach ($iblockElements as $enabledItemObject) { | |
$iblockElement = $enabledItemObject->get('IBLOCK_ID'); | |
\CIBlock::clearIblockTagCache($iblockElement); | |
} | |
\CIBlock::DisableClearTagCache(); | |
// Если композит включен, чистим его | |
if (\CHTMLPagesCache::IsCompositeEnabled()) { | |
\CHTMLPagesCache::CleanAll(); | |
} | |
} | |
return __CLASS__ . '::agent(' . $currentDateTime->getTimeStamp() . ');'; | |
} | |
public static function registerAgent() | |
{ | |
// Проверяем наличие уже зарегистрированного агента | |
$checkRegisterAgent = \CAgent::GetList([], [ | |
'NAME' => __CLASS__ . '%' | |
]); | |
// Если нашли агент | |
if ($checkRegisterAgent->Fetch()) { | |
return false; | |
} | |
// Регистрируем агент | |
\CAgent::AddAgent( | |
__CLASS__ . '::agent(0);', | |
null, | |
"N", | |
60 | |
); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment