使用Last-Modified头和OutputCacheAttribute在asp.net MVC 3客户端缓存 [英] Client side caching using Last-Modified header and OutputCacheAttribute in asp.net mvc 3

查看:153
本文介绍了使用Last-Modified头和OutputCacheAttribute在asp.net MVC 3客户端缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑

我要缓存在客户端上的图像和知道,有不同的方法来做到这一点在MVC 3:(纠正我,如果我错了)

I want to cache images on the client and know that there are different ways to do it in mvc 3: (correct me if I'm wrong)

1)你可以使用 OutputCacheAttribute 的帮助工作过期 HTTP标头。但它会返回 304未修改除非到期时间(即使图像被更改)。

1) You can use OutputCacheAttribute which works with the help of Expires http header. But it will return 304 Not Modified unless the time expire (even if the image was changed).

2)为了避免displaing陈旧的图像可以使用的Last-Modified HTTP标头(带有 OutputCacheAttribute )。在这种情况下,浏览器发送请求到服务器的If-Modified-Since的 HTTP标头。在服务器上,您验证该对象是否仍然有效与否,如果它是你刚刚返回的Last-Modified http头(和浏览器采用图像从本地缓存);如果该对象被修改,您用 200 OK 状态返回。结果
这样,浏览器需要请求每次从它自己的缓存服用图像之前发送到服务器。 这里是例子 -

2) To avoid displaing stale images You can use Last-Modified http header (with OutputCacheAttribute). In this case the browser sends the request to the server with If-Modified-Since http header. On the server You verify whether the object is still valid or not and if it is You just return Last-Modified http header (and the browser takes image from the local cache); if the object was modified You return it with 200 OK status.
So, the browser needs to send the request to the server each time before taking image from it's own cache. Here is the example -

3)还有另一种方法(我被告知我的情况下,以正确的方式,使图像将很少改变......无论如何,我需要实现正是这一点):要修改日期添加到图像URL和与设置缓存过期的永恒(1年以上)。如果图像已经改变了,您应该发送新网址的新版本。

3) There is another way (as I was told the right way in my case, cause the images will change very rarely... anyway, I need to implement exactly this): To add modified date to the image url and set caching with Expires for the eternity (1 year or more). If the image have changed You should send new url with new version.

下面是code:

public class LastModifiedCacheAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.Result is FilePathResult)
        {
            var result = (FilePathResult)filterContext.Result;
            var lastModify = File.GetLastWriteTime(result.FileName);
            if (!HasModification(filterContext.RequestContext, lastModify))
                filterContext.Result = NotModified(filterContext.RequestContext, lastModify);
            SetLastModifiedDate(filterContext.RequestContext, lastModify);
        }
        base.OnActionExecuted(filterContext);
    }

    private static void SetLastModifiedDate(RequestContext requestContext, DateTime modificationDate)
    {
        requestContext.HttpContext.Response.Cache.SetLastModified(modificationDate);
    }

    private static bool HasModification(RequestContext context, DateTime modificationDate)
    {
        var headerValue = context.HttpContext.Request.Headers["If-Modified-Since"];
        if (headerValue == null)
            return true;
        var modifiedSince = DateTime.Parse(headerValue).ToLocalTime();
        return modifiedSince < modificationDate;
    }

    private static ActionResult NotModified(RequestContext response, DateTime lastModificationDate)
    {
        response.HttpContext.Response.Cache.SetLastModified(lastModificationDate);
        return new HttpStatusCodeResult(304, "Page has not been modified");
    }
}

和我在Global.asax中注册了 LastModifiedCacheAttribute 并应用了以下OutputCacheAttribute我的操作方法。

And I registered the LastModifiedCacheAttribute in Global.asax and applied the following OutputCacheAttribute to my action method.

[HttpGet, OutputCache(Duration = 3600, Location = OutputCacheLocation.Client, VaryByParam = "productId")]
public FilePathResult GetImage(int productId)
{ // some code }

如果我使用上面的code好像浏览器不发送请求到服务器,而不是它只是拍摄图像从缓存中,除非持续时间没有结束。 (当我改变图像的浏览器不会显示最新版本)

If I use the code above seems like the browser doesn't send requests to the server, instead it just take images from the cache unless the duration is not ended. (When I change the image the browser doesn't display new version)

问题:

<强> 1)如何实现第三种方法中,以使浏览器将采取从客户端缓存图像(并且将不发送到每个就是了图像时的服务器),除非图像的响应被修改?结果
编辑:实际code将AP preciated

1) How to implement the third approach, so that the browser will take the images from client cache (and will not send the response to the server each time it wants the image) unless the image was modified?
edited: the actual code will be appreciated.

2)在code中的第一个图像请求时上面写到最后一次修改(不知道为什么)。如何写入文件的修改日期为上次修改?结果
编辑:这个问题涉及到第二个方法。另外,如果我在客户端上只缓存并使用的Last-Modified 实施我得到 304未修改状态只有当我preSS F5 。如果我重新输入相同的URL我将获得 200 OK 。如果我在客户端上缓存,而无需使用的Last-Modified 它总是会返回 200 OK 不管是什么。怎么会这样来解释?

2) In the code above the time of the first image request is written to the Last-Modified (don't know why). How to write the modification date of the file into Last-Modified?
edited: this question relates to second approach. Also, if I cache only on the client and use Last-Modified implementation I get 304 Not Modified status only if I press F5. If I reenter the same url I will get 200 OK. If I cache on a client without using Last-Modified it will always return 200 OK no matter what. How could this be explained?

推荐答案

您可以考虑使用的ETag( HTTP ://en.wikipedia.org/wiki/HTTP_ETag ),这就是我想读你的问题的第一件事

You could look into using ETags (http://en.wikipedia.org/wiki/HTTP_ETag), that's the first thing I thought of reading your question.

您也可以看看这里:设置的ETag为FileResult - MVC 3

这篇关于使用Last-Modified头和OutputCacheAttribute在asp.net MVC 3客户端缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆