Categories and Caching

Akram Shaker asked on November 14, 2024 13:13

Does changing the categories of a page in kentico xperience 13 not count as a page change that will flush the cache when using

$"nodes|{SiteContext.CurrentSiteName}|{Blog.CLASS_NAME}|all" 

public async Task<IEnumerable<Blog>> GetCategoryBlogsAsync(IEnumerable<string> categories, string cultureCode)
{
  var blogs = await GetAllBlogsAsync(cultureCode);
    return CacheHelper.Cache(cs =>
    {
      cs.CacheDependency = CacheHelper.GetCacheDependency($"nodes|{SiteContext.CurrentSiteName}|{Blog.CLASS_NAME}|all");
      return blogs.Where(x =>
        {
          var blogCategories = x.Categories.CodeNames
              .Select(x => x.Value.ToString())
              .WhereNotNull();
          return blogCategories.Any(y => !string.IsNullOrWhiteSpace(y) && categories.Contains(y));
      });
    }, new CacheSettings(1440, Blog.CLASS_NAME, "CategoryBlogsAsync", string.Join(",", categories), cultureCode));
  }

public async Task<IEnumerable<Blog>> GetAllBlogsAsync(string cultureCode)
{
    var blogs = await _pageRetriever.RetrieveAsync<Blog>(
        query => query
          .Columns(BlogColumns)
          .WithPageUrlPaths()
          .Published()
          .PublishedVersion()
          .Culture(cultureCode)
          .OnCurrentSite()
          .FilterDuplicates(),
        cache => cache
          .Key($"{nameof(BlogsService)}|{nameof(GetAllBlogsAsync)}|{cultureCode}")
          .Dependencies((_, builder) => builder.Custom($"nodes|{SiteContext.CurrentSiteName}|{Blog.CLASS_NAME}|all")));

    return blogs;
}

Recent Answers


Juraj Ondrus answered on November 14, 2024 15:21

Categories are different object type and due to old design, done through a binding table, the actual change is done in the documentcategory object type. So, if you want to clear cache when new category is added (to any page) you can use cms.documentcategory|all key

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.