Kentico 13 Caching

Matthew Butler asked on June 8, 2022 12:54

I'm looking at kentico 13 caching and have 2 questions:

  1. I'm using CacheHelper.Cache with name parts including "test|data|navigation", but I can't see that in the debug application, although cache appears to be working and other cache keys are listed. why wouldn't it show?

  2. The <cache tag helper appears to be cleared when the dependencies within the viewcomponent inside the tag are touched. does the cache tag automatically detect dependencies within?

Many Thanks

Correct Answer

Sean Wright answered on June 9, 2022 15:03

Matthew,

I still use CacheHelper.GetCacheDependency since this is a stateless utility method.

In regards to MemoryCache, yes, it inherits child dependencies (change tokens) and that is intentional (it's by design in .NET), but Xperience doesn't add any cache dependencies by default (I though that's what you were asking).

If you are interested in a cache decoration solution for KX13, I've released some NuGet packages. This also includes a custom Cache Tag Helper that tracks child dependencies automatically (it doesn't need <cache-dependency>).

0 votesVote for this answer Unmark Correct answer

Recent Answers


Sean Wright answered on June 9, 2022 05:08

  1. Where is the cached data? Is it in the CMS or the ASPNET Core application? I would recommend using IProgressiveCache which is an abstraction over CacheHelper.Cache and is the best option for new applications. There is a known limitation where cache items won't appear in the Cache Debug UI in the CMS if the ASPNET Core app is running under a different domain than the primary presentation URL defined for the site.
  2. Nothing is automatically detected, but the <cache-dependency> tag helper can be assigned cache dependency keys, which are propagated up to the <cache> tag helper. Any data changes that touches those keys will cause the <cache> tag helper's cached content to be invalidated. If you are working with a Widget, it's output can be cached by assigning cache dependency keys to the ComponentViewModel.CacheDependencies.CacheKeys property.
0 votesVote for this answer Mark as a Correct answer

Matthew Butler answered on June 9, 2022 09:07 (last edited on June 9, 2022 10:32)

Thanks for the reply Sean, I've read several of your blogs, and really like your general approach to coding.

  1. I was using CacheHelper, but will switch to IProgressiveCache, is there any way to easily debug? Also what do you replace CacheHelper.GetCacheDependency with?

  2. It seems to be automatically dependant on and caches within the tag, as I haven't set the <cache-dependency> and it clears all the time the child cache item gets cleared. and looking at the <cache-dependency> thats how it appears to work

            IChangeToken changeToken = CacheDependencyAdapter.GetChangeToken(CacheKeys);
            string key = Guid.NewGuid().ToString();
            MemoryCache.Set<object>(key, null, changeToken);
            MemoryCache.Remove(key);
    

Also Sean I've implemented your QueryHandlerCacheDecorator and like your approach to the OutputCache dependencies, but I'm using K13 .net core, so wondered if you had any thoughts to a similar approach in K13?

0 votesVote for this answer Mark as a Correct answer

Matthew Butler answered on June 9, 2022 10:07

So I tried a contrived example, and it appears nested IMemoryCache inherit child dependencies. Calling TouchKey on "testing" invalidates "outertestkey" cache.

 var cachedValue = _memoryCache.GetOrCreate("outertestkey",
                            cacheEntry =>
                            {
                                var changeToken = _cacheDependencyAdapter.GetChangeToken(new[] { "testing" });
                                string key = Guid.NewGuid().ToString();
                                _memoryCache.Set<object>(key, null, changeToken);
                                _memoryCache.Remove(key);


                                return DateTime.Now;
                            });

      CacheHelper.TouchKey("testing"); 
0 votesVote for this answer Mark as a Correct answer

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