Hi,
We are building a solution with some extra cashing functionality, but we are having issues when trying to handle/override the caching policy.
The issue is that it only works sometimes, and sometimes it simply stops working. It seems like Dynamicweb override the caching different places.
Do anybody have a nice tip of where to override the cache in order to control the caching?
Snippet:
/// <summary>
/// Set cache header. We do this as early as possible but after Dynamicweb has resolved the URL to ID
/// </summary>
[
Subscribe(Dynamicweb.Notifications.Standard.Application.BeforePreRequestHandlerExecute),
Subscribe(Dynamicweb.Notifications.Standard.Page.OnGlobalTags)
]
public class SetCacheNotificationSubscriber : NotificationSubscriber
{
public override void OnNotify(string notification, NotificationArgs args)
{
if (!Int32.TryParse(System.Web.HttpContext.Current.Request.QueryString["ID"] ?? "", out var pageId))
return;
var cacheDuration = CachingService.GetCacheDuration(pageId);
if (cacheDuration.TotalMinutes > 0)
{
context.Response.Headers["norriq-cache"] = cacheDuration.ToString();
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.Add(cacheDuration));
context.Response.Cache.SetValidUntilExpires(true);
}
else
{
context.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
context.Response.Cache.SetValidUntilExpires(false);
context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();
}
}
}