asp.net - ASP.NET 4.0 中不同的服务器和客户端缓存策略

标签 asp.net outputcache duration

我缺少对 ASP.NET 输出缓存的一些基本了解。

就我而言,我的资源与我的 VaryByCustom 密切相关。 key 。在服务器端,我希望这些可以无限期地缓存,直到该键更改为止。没有理由在计时器上清除这些缓存的条目。

但是,客户端应该每小时 checkin 一次,以获取服务器认为是最新鲜的内容。

如果我将持续时间设置为 1 小时,我知道在客户端上正确设置了过期 header 。但它也驱逐服务器端缓存吗?有没有办法确保响应在我的 VaryByCustom 更改之前保持缓存在服务器上,但仍然允许更频繁的客户端到期?

最佳答案

这里要注意的重要一点是,如果您的 VaryByCustom 更改,asp.net 会存储页面的单独缓存版本。

因此,假设您的 VaryByCustom-"custom1"与 Duration="60"那么这将被缓存 60 秒。

如果您的 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();
        }

我没有在 aspx 中包含 asp:Label 的标记,但我希望你能大致了解。

关于asp.net - ASP.NET 4.0 中不同的服务器和客户端缓存策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6207724/

相关文章:

javascript - 如果用户空闲或没有事件,asp.net 刷新页面

asp.net - 用于 ASP.NET 的 jQuery 图表控件

asp.net - 通过 clientID 改变用户控件上的输出缓存

android - 将 Android Toast 持续时间设置得非常长(例如 1 分钟)

c# - 数据表 : Does it retrieve all data at once or when it is needed

asp.net - .NET 开发人员应该了解 MSBuild 什么?

asp.net-mvc - 如何最好地使用 ASP.NET MVC 中的 Expires header ?

asp.net-mvc - Razor View 引擎 .cshtml 页面中的 OutputCache

sql - SQL Server Profiler 中的 CPU 与持续时间

javascript - 如何在上传时获取多个视频的时长?