使浏览器缓存失效

发布于 2024-08-13 09:02:02 字数 616 浏览 3 评论 0原文

我有一个与缓存失效技术相关的问题...我正在尝试为我的网站实现一种机制,自动使浏览器缓存(css、图像...)失效。每当我更新网站(更改图像或样式)时,我希望能够以编程方式使浏览器缓存失效;

例如:对于当前版本,CSS 和一些图像已发生更改。在这种情况下,我希望更新完成后,当用户向网站执行请求时,其浏览器的缓存自动失效,从而强制重新下载新的图像和样式。这应该只针对客户端的第一个请求执行...应该从缓存中检索以下请求(因此设置 no-cache pragma 是不可能的)。

这是我尝试过的: 在 BeginRequest 事件处理程序中,我添加了以下几行:

Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
Response.Cache.SetETag("\"e111293b17594f1487d136ea7e9314ac\"");

这会在响应标头中设置 ETag。我想如果我在每次发布时更改这个 ETag,并在每次请求时设置它,缓存就会失效,但似乎并非如此。我使用 Live HTTP 标头来查看结果,并且 ETAG 已为响应正确设置,但 css 和图像仍然从缓存中获取。

我有什么想法可以实现这一点,或者是否可以实现?

I have a question related to cache invalidation techniques... I am trying to implement a mechanism for my website, that automatically invalidates browser cache (css, images...). I want to be able to programatically invalidate browser cache, whenever I update the website (change images or styles);

For example: for the current release, among others, the css and some of the images have changed. In this situation I want that after the update is finished, when a user performs a request to the website, his browser's cache to get automatically invalidated, thus forcing the re-download of the new images and styles. This should be done only for the client's first request... the following ones should be retrieved from cache (so setting the no-cache pragma is out of the question).

Here's what i've tried:
in the BeginRequest event handler, I added the following lines:

Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
Response.Cache.SetETag("\"e111293b17594f1487d136ea7e9314ac\"");

this sets the ETag in the response headers. I figured that if I change this ETag at each release, and set it at each request, the cache will be invalidated, but it seems that it is not. I used Live HTTP headers to see the results and the ETAG is correctly set up for the response, but the css and images are still taken from cache.

Any ideas of how I could accomplish this, or if it can be accomplished at all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紧拥背影 2024-08-20 09:02:02

我过去也遇到过这样的问题。不幸的是,我找不到一个真正好的方法来完成这个任务,所以我不得不想出一个解决方法。我只处理 CSS 文件的这个问题,因此我向每个 CSS 引用添加了一个额外的查询字符串参数,例如,

 <link rel="stylesheet" type="text/css" 
       href="default.css?buildnumber=<%= Buildnumber %>" />

构建号随着每个版本的发布而增加,因此浏览器被迫去寻找这个新文件。这不是一个理想的解决方案,但它运行顺利。

I have run into issues like this in the past. Unfortunately I couldn't find a really nice way to accomplish this so I had to come up with a workaround. I was only dealing with this issue for CSS files so I added an extra querystring parameter to every CSS reference, for example

 <link rel="stylesheet" type="text/css" 
       href="default.css?buildnumber=<%= Buildnumber %>" />

The build number gets incremented with each release so the browser was forced to go look for this new file. Not an ideal solution, but it worked without a hitch.

素食主义者 2024-08-20 09:02:02

对于那些正在寻求 MVC5 解决方案的人:

步骤 1:将项目的 AssemblyInfo.cs 文件更改为以下步骤

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.*")]

步骤 2:创建一个类以获取项目的当前版本信息:

     public class Versioner
        {
            public static Version GetVersion()
            {
                Assembly thisAssem = typeof(Versionador).Assembly;
                AssemblyName thisAssemName = thisAssem.GetName();
                Version vrs = thisAssemName.Version;
    
                return vrs;
            }
    


//Not really necessary, just if you need to show this info
            public static string GetDataBuild()
            {
                Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
                DateTime buildDate = new DateTime(2000, 1, 1).AddDays(version.Build).AddSeconds(version.Revision * 2);
                string displayableVersion = $"{version} ({buildDate})";
    
                return displayableVersion;
            }
        }

步骤 3:在视图中调用类方法新版本需要缓存自动刷新。

    @{
    Version _ver = <MyProject>.Classes.Extensions.Versioner.GetVersion();
    }
//here, <MyProject>.Classes.Extensions is my path to the Versioner.cls class file, adjust it to your project's classes path

步骤 4:使用带有版本字符串的变量附加到脚本或 .css 文件

 <script src="~/js/index.js?v=@_ver"></script>
 <link href="/css/style.css?v=@_ver" rel="stylesheet" />

For those who are seeking for a MVC5 solution:

Step1: Change the AssemblyInfo.cs file of the project to the following

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.*")]

Step 2: Create a class to get the current version info on your project:

     public class Versioner
        {
            public static Version GetVersion()
            {
                Assembly thisAssem = typeof(Versionador).Assembly;
                AssemblyName thisAssemName = thisAssem.GetName();
                Version vrs = thisAssemName.Version;
    
                return vrs;
            }
    


//Not really necessary, just if you need to show this info
            public static string GetDataBuild()
            {
                Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
                DateTime buildDate = new DateTime(2000, 1, 1).AddDays(version.Build).AddSeconds(version.Revision * 2);
                string displayableVersion = 
quot;{version} ({buildDate})";
    
                return displayableVersion;
            }
        }

Step 3: Call the class methods in the views which needs cache auto-refresh on new builds.

    @{
    Version _ver = <MyProject>.Classes.Extensions.Versioner.GetVersion();
    }
//here, <MyProject>.Classes.Extensions is my path to the Versioner.cls class file, adjust it to your project's classes path

Step 4: Use the variable with the version string to append to your scripts or .css files

 <script src="~/js/index.js?v=@_ver"></script>
 <link href="/css/style.css?v=@_ver" rel="stylesheet" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文