Not Able to Implement MVC OutputCache K12 Example

Jared Hughes asked on May 14, 2019 19:00

Hello,

I have attempted to implement the MVC OutputCache example from the K12 documentation. The MVC output caching is working as intended, but I am struggling to observe the TouchKey behavior. In other words, if we update the Kentico page correlating to the dependencyCacheKey, the output remains cached for the duration specified in the MVC OutputCache attribute.

Here is my action method:

[OutputCache(Duration = 120, Location = OutputCacheLocation.Server, VaryByParam = "none")]
    public ActionResult Index()
    {
        Home home = homeRepository.GetHome();

        //// nodes|<site name>|<page type code name>|all
        //string dependencyCacheKey = $"nodes|{SiteContext.CurrentSiteName}|{home.ClassName}|all".ToLowerInvariant();

        //// node|<site name>|<alias path>
        //string dependencyCacheKey = $"node|{SiteContext.CurrentSiteName}|{home.NodeAliasPath}".ToLowerInvariant();

        //// nodeguid|<site name>|<node guid>
        //string dependencyCacheKey = $"nodeguid|{SiteContext.CurrentSiteName}|{home.NodeGuid}".ToLowerInvariant();

        // nodeid|<node id>
        string dependencyCacheKey = $"nodeid|{SiteContext.CurrentSiteName}|{home.NodeId}".ToLowerInvariant();


        CMS.Helpers.CacheHelper.EnsureDummyKey(dependencyCacheKey);
        HttpContext.Response.AddCacheItemDependency(dependencyCacheKey);

        // Create a new HomeViewModel instance based on the page data
        HomeViewModel homeModel = new HomeViewModel(home);

        return View(homeModel);
    }
}

Additional, we are not seeing the dependency key in Debug->Cache Items after invoking CacheHelper.EnsureDummyKey() (perhaps this is by design).

Correct Answer

Mike Wills answered on May 14, 2019 20:17

Hi Jared,

Did you already verify that the MVC application is configured as a member of the Kentico Web Farm?

Mike

0 votesVote for this answer Unmark Correct answer

Recent Answers


Dmitry Bastron answered on May 14, 2019 20:06

Hi Jared,

If you check DancingGoatMVC example website output cache dependencies are only on type (Page Types or Custom Classes):

public void AddDependencyOnPages<T>() where T : TreeNode, new()
{
    if (!mCacheEnabled)
    {
        return;
    }

    var className = mContentItemMetadataProvider.GetClassNameFromPageRuntimeType<T>();
    var dependencyCacheKey = String.Format("nodes|{0}|{1}|all", SiteContext.CurrentSiteName.ToLowerInvariant(), className);

    AddCacheItemDependency(dependencyCacheKey);
    AddCacheItemDependency("cms.adhocrelationship|all");
    AddCacheItemDependency("cms.relationship|all");
}

I think it might not be working with individual objects with this format

string dependencyCacheKey = $"nodeid|{SiteContext.CurrentSiteName}|{home.NodeId}".ToLowerInvariant();

I see in your code a couple of commented examples with "nodes|..|all" dummy key. Are they working for you?

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on May 14, 2019 20:24

Yes, Mike is right, Web Farms should be also configured according to this and this article. Automatic web farm works perfectly for me.

Also, as per my knowledge, admin area Debug application seems to be showing cache items from CMS (not MVC) project.

0 votesVote for this answer Mark as a Correct answer

Jared Hughes answered on May 14, 2019 20:54

Dmitry - I did try those commented examples, as well.

Mike - No, I did not verify the MVC application is configured as a member of Kentico Web Farm. I am running the application on my local IIS Express instance (localhost) via the IDE. I added "New Server" via the Kentico Web Farm application and matched the server name to the CMSWebFarmServerName appsetting in my web.config. I see periodic messages in the event log indicating [server_name] IS HEALTHY. However, the Status value for the new server in Web Farm application is "Not responding". All this to say, still not functioning for me.

Thank you both for the input - any additional feedback is greatly appreciated!

0 votesVote for this answer Mark as a Correct answer

Jared Hughes answered on May 14, 2019 20:55

Dmitry - I did not see your most recent comments until now... Let me investigate. Thanks!

0 votesVote for this answer Mark as a Correct answer

Jared Hughes answered on May 14, 2019 21:27

Everything works as intended. Wow - thanks, guys!

I am not exactly sure why I was struggling when posting my last couple comments. I read through the posts provided by Dmitry, but they did not lead me to make any changes. I fired it up to try some new things and it worked. Perhaps I needed a wait a few minutes for the CMS to process the Web Farm updates. Also, there are other developers running the application locally, using the same web.config, so perhaps there was a collision with multiple web farm servers responding with the same name.

0 votesVote for this answer Mark as a Correct answer

Mike Wills answered on May 14, 2019 21:55

Hi Jared, that could be. If two application instances use the same CMSWebFarmServerName it will break web farm synchronization. For that reason, I created a custom implementation if IAppSettingsService, so that I could ensure every app instance used a unique name.

0 votesVote for this answer Mark as a Correct answer

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