November Happy Hour will be moved to Thursday December 5th.

Object Caching vs LinkItemCollection

Vote:
 

Hi all, 

I yesterday stubled on some strange behaviour in a CMS 12 (latest packages) website I'm working on. 

Behaviour:
On a specific page there is a LinkItemCollection property that is used to show some links to other parts of the site.
After a restart the list would not show any links to the visitor, but when I view the page in the CMS in preview mode the links are visible. After a publish the links are visible to the visitor, and after a restart they are gone again.

What I found out so far:

It seems that caching plays a part in this. If I load the page, check if the collection is null and then
invalidate the cache and load the page again the links are there. 

var contentRepo = ServiceLocator.Current.GetInstance<IContentLoader>();
if (page.QuickNavigationLinks == null)
{
    var ccr = ServiceLocator.Current.GetInstance<IContentCacheRemover>();
    ccr.Remove(page.ContentLink);

    var newLoad = contentRepo.Get<SubHomePage>(currentPage.ContentLink);
    if (newLoad.QuickNavigationLinks != null)
    {
        // Now the links are there
    }
}



Then I tried to find out, why an incomplete page was cached. I implemented my own ObjectInstanceCache to intercept the page being cached, and use the callstack to find the origin.

public class MyObjectInstanceCache : IObjectInstanceCache
{
    private readonly IMemoryCache _memoryCache;

    public MyObjectInstanceCache(IMemoryCache memoryCache)
    {
        _memoryCache = memoryCache;
    }

    public void Clear()
    {
        // Do nothing
    }

    public object Get(string key)
    {
        return _memoryCache.Get(key);
    }

    public void Insert(string key, object value, CacheEvictionPolicy evictionPolicy)
    {
        if (key.Equals("EPPageData:62694") && value is SubHomePage page && page.QuickNavigationLinks == null)
        {
            // Here the page is cached without QuickNavigationLinks
            _memoryCache.Set(key, value);   
        }

        _memoryCache.Set(key, value);
    }

    public void Remove(string key)
    {
        _memoryCache.Remove(key);
    }
}


It seems now, that on loading the homepage. The children (including my page) of the homepage are loaded:

var loader = ServiceLocator.Current.GetInstance<IContentLoader>();
return loader.GetChildren<IContent>(parentPage.ContentLink);


And in loading this children I see the page being cached without the QuickNavigationLinks.
Then when I navigate to this page, this page is loaded from cache, and no links are shown.

My questions:

Is this something that makes sence?
Am I missing something here?
Why is the page cached differently when loading is from GetChildren in comparison to the normal Get of the ContentLoader? 

#333297
Nov 22, 2024 11:08
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.