Developer forum

Forum » CMS - Standard features » Frontend caching issues in Dynamicweb 9+

Frontend caching issues in Dynamicweb 9+

Søren Heide Larsen
Søren Heide Larsen
Reply

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();
		}
	}
}

Replies

 
Nicolai Pedersen
Reply

Hm - that will be a hard find.

We might have some response.expires = -1 some places - and similar things.
DW has a feature to enable cache headers - and as a cause of that, we also have places that we clear them again if the feature running on that page should not be cached - i.e. on carts, discount calculations etc.

You could move the notification to onafteroutput - then we should be done manipulating the response.

BR Nicolai

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

I would set the cache headers as late as possible to make sure that it is not overridden.

The last chance to modify response headers is at the Dynamicweb.Notifications.Standard.Application.BeforeEndRequest notification.

Another option is to create and register a custom HTTP module which handles the PreSendRequestHeaders event.

Make sure that you only cache when the HTTP status code is 200 OK and that you don't cache any output which contains user specific content (user profile, content with permissions, product prices, etc.).

 
Søren Heide Larsen
Søren Heide Larsen
Reply

Thank you for the replies, I will let you know how it goes - see you Thursday :)

 

You must be logged in to post in the forum