Create a 3 day cache

Roberto Randall Riquelme 0 Reputation points
2023-06-24T16:14:26.8766667+00:00

If I want to create a 3 day cache to put on a page, is this correct?

Response.Expires = 4320

Response.ExpiresAbsolute = DateAdd("d", 3, Now())

Response.AddHeader "pragma","public"

Response.AddHeader "cache-control","public,max-age=259200"

Response.CacheControl = "public"

I am not sure if this code is correct so that the page where I want to put it has a cache of 3 days para CDN Azure.

Thank you.

Azure Front Door
Azure Front Door
An Azure service that provides a cloud content delivery network with threat protection.
851 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sedat SALMAN 14,180 Reputation points MVP
    2023-06-24T23:52:17.25+00:00

    There are a few points that might need adjustment

    you can also check the following ref document

    https://learn.microsoft.com/en-us/dotnet/api/system.web.httpresponse.expires?view=netframework-4.8.1

    https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms526058(v=vs.90)

    https://learn.microsoft.com/en-us/previous-versions/iis/6.0-sdk/ms524327(v=vs.90)

    https://learn.microsoft.com/en-us/azure/cdn/cdn-manage-expiration-of-cloud-service-content

    but in summary

    Response.Expires = 4320: This sets the number of minutes before a page cached on a browser expires to 4320 minutes (which equates to 3 days)​<sup>.</sup> However, the Expires property is provided for compatibility with earlier versions of ASP and has been deprecated in favor of the methods of the HttpCachePolicy class​.

    Response.ExpiresAbsolute = DateAdd("d", 3, Now()): The ExpiresAbsolute property specifies the date and time at which a page cached on a browser expires. If a user returns to the same page before that date and time, the cached version is displayed. The value you've set here seems to correctly set the expiration date to 3 days from the current time​. Like the Expires property, ExpiresAbsolute has also been deprecated in favor of the methods of the HttpCachePolicy class​.

    Response.AddHeader "pragma","public" and Response.AddHeader "cache-control","public,max-age=259200": The AddHeader method adds a new HTML header and value to the response sent to the client. In your case, you're setting the pragma and cache-control headers. For cache-control, the max-age directive is set to 259200 seconds, which is equivalent to 3 days. This seems correct, but it's generally recommended to use methods that can provide the functionality you need if they exist. For example, to set cache control for a response, you could use Response.CacheControl​.

    Response.CacheControl = "public": This sets the CacheControl property to "public", which allows the page to be cached by shared (public) caches, private caches, and the client's browser. This seems correct but I wasn't able to fully confirm this due to time constraints.

    If you're working with a .NET application, you might want to consider using the HttpResponse.Cache property of the .NET API to control the CDN caching behavior​. For static content, you can control the update frequency by modifying the applicationHost.config or Web.config configuration files for your web application​.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.