Various server and client cache policies in ASP.NET 4.0

I lack a fundamental understanding of ASP.NET output caching.

In my case, I have resources that are very much tied to my key VaryByCustom. On the server side, I would like them to be cached indefinitely until this key changes. There is no reason for these cached entries to be cleared by a timer.

Clients, however, must check once an hour to get what the server considers the most recent.

If I set my duration to 1 hour, I know that it sets the validity header on the client correctly. But does the server cache also exclude? Is there a way to ensure that the response remains cached on the server until my VaryByCustom changes, but still allows more frequent client-side expiration?

+5
source share
1 answer

It is important to note that if your VaryByCustom changes, asp.net stores a separate cached version of the page.

So, suppose your VaryByCustom is "custom1" with Duration = "60", then this will be cached for 60 seconds.

, VaryByCustom "custom2" = "60", asp.net , "custom1", 60 .

, VaryByCustom .

Global.asax

public override string GetVaryByCustomString(HttpContext context, string custom)
        {
            if (custom == "browser")
            {
                string browserName;
                browserName = Context.Request.Browser.Browser;
                browserName += Context.Request.Browser.MajorVersion.ToString();

                return browserName;
            }
            else
            {
                return base.GetVaryByCustomString(context, custom);
            }

        }

Aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebFormsPlayGround._Default" %>
    <%@ OutputCache Duration="40" VaryByParam="None" VaryByCustom="browser" %>

protected void Page_Load(object sender, EventArgs e)
        {
            lblTime.Text = "The time now is: <br/>";
            lblTime.Text += DateTime.Now.ToString();
        }

asp: aspx, , .

0

All Articles