Caching question using Xperience 13

Danny Winbourne asked on November 17, 2020 10:40

I am looking over caching methods in Kentico 13 .net Core. Specically looking at the RepositoryCacheHelper class, and CachePages method.

My current use case is wanting to cache all pages of a given PageType, and invalidate the cache if any of the pages change.

The below works well - List stays cached until I make a change to one of the items (I did a test where I changed value in database, and it didn't update in code as expected).

                var otherGroupItems = pageRetriever.Retrieve<NavigationGroup>(
                   query => query
                       .WhereEquals(nameof(NavigationGroup.ClassName), NavigationGroup.CLASS_NAME), 
                   cache => cache
                       .Key($"node|{SiteContext.CurrentSiteName}|{NavigationGroup.CLASS_NAME}|all")
                         .Dependencies((_, builder) => { }, true)
                         ).ToList();

I then tried doing something similar using RepositoryCacheHelper

                    var other2 = repositoryCacheHelper.CachePages(() =>
                {
                    var otherGroupItems = pageRetriever.Retrieve<NavigationGroup>(
                       query => query
                           .WhereEquals(nameof(NavigationGroup.ClassName), NavigationGroup.CLASS_NAME)).ToList();


                    return otherGroupItems;

                }, $"{nameof(NavigationRepository)}|{nameof(GetMainNavigation)}", new[]
                {
                    $"node|{SiteContext.CurrentSiteName}|{NavigationGroup.CLASS_NAME}|all"
                });

When a change in the CMS is made, the cache here is not cleared. I think I must have done something wrong with my dependency keys, but any tips greatly appreciated.

Correct Answer

Dmitry Bastron answered on November 17, 2020 10:55

Hi Danny,

You were almost there! All you need to do is to use "nodes" but not "node" in cache dependency key:

$"nodes|{SiteContext.CurrentSiteName}|{NavigationGroup.CLASS_NAME}|all"  // This is correct
$"node|{SiteContext.CurrentSiteName}|{NavigationGroup.CLASS_NAME}|all"   // This will not work

Please refer to the documentation where these key formats are explained.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Danny Winbourne answered on November 17, 2020 11:03

Thanks for the quick response Dmitry. That was it!!

Strange how it worked for the first example, but now I know.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on November 17, 2020 11:12

On the first example when you pass "true" in the .Dependencies() method, Kentico adds default dependencies automatically for you, and this key based on item type is one of the default dependency keys.

Also, please bear in mind in your first example when you use .Key() method you define your unique cache key (the data is stored against that key), but not cache dependency key (which are used to invalidate the cache).

0 votesVote for this answer Mark as a Correct answer

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