Контекст при создании кастомного кеша

Хочу кешировать вывод снипета из админки, при редактировании ресурсов (не знаю правильно это или нет?).
Делаю так:
<?php
$modx->cacheManager->delete('newLotsIndex', array(xPDO::OPT_CACHE_KEY=>'custom_cache'));
$date_from = strtotime('-2 month');
$res = $modx->runSnippet('msProducts', array(
	'parents' => 2,
	'limit' => 16,
	'where' => ["publishedon:>" => $date_from, "Data.remains:>=" => 1 ],
        'sortby' => '{"publishedon": "desc"}',
        'tpl' => '@FILE chunks/tovarPreview4row.tpl',
        'includeThumbs' => '140x140'
));
$modx->cacheManager->set('newLotsIndex', $res, 0, array(xPDO::OPT_CACHE_KEY=>'custom_cache'));
Все работает, но в логи забиваются этим:
[2024-01-19 06:12:41] (INFO in modContext::makeUrl @ /var/www/xxxx/www/core/model/modx/modcontext.class.php : 325) Resource with id 140625 was not found in context mgr
[2024-01-19 06:12:41] (WARN @ /var/www/xxxx/www/core/model/modx/modcontext.class.php : 248) `140623` was requested but no alias was located.
[2024-01-19 06:12:41] (INFO in modContext::makeUrl @ /var/www/xxxx/www/core/model/modx/modcontext.class.php : 325) Resource with id 140623 was not found in context mgr
[2024-01-19 06:12:41] (WARN @ /var/www/xxxx/www/core/model/modx/modcontext.class.php : 248) `140623` was requested but no alias was located.


Как можно это исправить? Где указать контекст? Заранее спасибо.
ViktorK
19 января 2024, 09:22
modx.pro
293
0

Комментарии: 4

Володя
19 января 2024, 10:48
+1
в начале попробуй добавить
$modx->switchContext('web');
    Володя
    19 января 2024, 10:51
    +1
    но вообще как то странно, я бы сделал сниппет обертку для сохранения в кеш, чтото типа
    {var $output= 'saveToCache'|snippet:[
        'element' => 'msProducts',
        'cacheKey' => 'cacheKey',
        'cacheTime' => 3600
        ....
    ]}
    {$output}
      Володя
      19 января 2024, 10:54
      +1
      ну и сам сниппет примерно такой
      <?php
      
      /** @var modX $modx */
      /** @var array $scriptProperties */
      
      $cacheKey = $modx->getOption('cacheKey', $scriptProperties, 'cache_key', true);
      $cacheTime = $modx->getOption('cacheTime', $scriptProperties, 3600);
      
      /** @var modCacheManager $cacheManager */
      $cacheManager = $modx->getCacheManager();
      if ($output = $cacheManager->get($cacheKey)) {
          return $output;
      }
      
      // если нет в кеше получаем $output и сохраняем в кеш
      
      $cacheManager->set($cacheKey, $output, $cacheTime);
      
      return $output;
        ViktorK
        19 января 2024, 22:25
        0
        Володя, большое спасибо! Попробую так сделать.
      Авторизуйтесь или зарегистрируйтесь, чтобы оставлять комментарии.
      4