OutputCache Implementation Help

Vote:
 

Hi there,

Trying to set up outputcache but failing so far.

I have ensured that I have the following in my web.config:

<caching>
    <outputcache enableoutputcache="true"/>
    <outputcachesettings>
        <outputcacheprofiles>
            <add name="ClientResourceCache" enabled="true" duration="3600" varybyparam="*" varybycontentencoding="gzip;deflate" />
        </outputcacheprofiles>  
    </outputcachesettings>      
</caching>

And:

<applicationsettings httpcacheability="Public" pagevalidatetemplate="false" uishowglobalizationuserinterface="true" uiurl="~/EPiServer/CMS/" urlrebasekind="ToRootRelative" uieditorcsspaths="~/Static/css/Editor.css" uisafehtmltags="b,i,u,br,em,strong,p,a,img,ol,ul,li">
</applicationsettings>

Next, I added [ContentOutputCache] to my StartPageController.cs like so:

[ContentOutputCache]
public class StartPageController : PageControllerBase
{
public ActionResult Index(StartPage currentPage)

{

var model = PageViewModel.Create(currentPage);

if (SiteDefinition.Current.StartPage.CompareToIgnoreWorkID(currentPage.ContentLink)) // Check if it is the StartPage or just a page of the StartPage type.
{
//Connect the view models logotype property to the start page's to make it editable
var editHints = ViewData.GetEditHints, StartPage>();
editHints.AddConnection(m => m.Layout.Logotype, p => p.SiteLogotype);
editHints.AddConnection(m => m.Layout.Footer, p => p.FooterBlock);
}

return View(model);
}
}

But it does not appear to be working. I have ensured that I am not logged in during testing. But when looking at the headers in the browser I see under cache-control: private.

In the article it mentions:

The next part of the process is to enable the response headers on your page.  If you don't do this you will see a no-cache set in your HTML requests response header.  To do this is pretty simple.  At some point on a page load you need to add the following code:

public void SetResposneHeaders()
        {
            HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddMinutes(2.0));
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
            HttpContext.Current.Response.Cache.SetValidUntilExpires(true);
        }

But I am unsure where to place this bit, and how to call it.

#199283
Nov 22, 2018 12:29
Vote:
 

You could add it to the controller you use the attribute on.

I would use the settings from Episerver though, for the values:

HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.Add(Settings.Instance.HttpCacheExpiration));
HttpContext.Current.Response.Cache.SetMaxAge(Settings.Instance.HttpCacheExpiration);
HttpContext.Current.Response.Cache.SetCacheability(Settings.Instance.HttpCacheability);
HttpContext.Current.Response.Cache.SetValidUntilExpires(true);
#199303
Nov 22, 2018 14:00
Vote:
 

Hi thanks for this. I notice that max-age is 0 in the browsers headers.

Are these settings taken from the web.config file? If yes, I do not see anything in the web.config file called HttpCacheExpiration ?

Or is max-age=0 correct ??

#199309
Edited, Nov 22, 2018 14:41
Vote:
 

The settings are taken from the applicationsettings in the Episerver node in the web.config. There you should set the httpCacheExpiration value

#199315
Nov 22, 2018 15:02
Vote:
 

Thanks so much. I have added httpCacheExpiration="3600" to the web.config.

Interestingly when I check the headers in the browser I get max-age=31536000 which is 1 year if I am not mistaken. Is my value for httpCacheExpiration being misread, or is it supposed to be in ticks or something like that ? 

#199358
Nov 23, 2018 11:00
Vote:
 

It's a timespan, so for an hour I think it would be 1:00:00

#199359
Nov 23, 2018 11:15
Vote:
 

Thanks!

#199363
Nov 23, 2018 11:52
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.