如何设置不同的缓存到期时间为客户端和服务器缓存 [英] How to set different Cache expire times for Client and Server caches

查看:139
本文介绍了如何设置不同的缓存到期时间为客户端和服务器缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一定的网页有客户端10分钟的高速缓存和24小时的服务器。原因是如果页面变化,客户端将获取10分钟内更新的版本,但是如果没有任何变化,服务器将只需要重建页面,每日一次。

I would like to have certain pages have a 10 minute Cache for clients and 24 hours for the server. The reason is if the page changes, the client will fetch the updated version within 10 minutes, but if nothing changes the server will only have to rebuild the page once a day.

问题是,输出缓存设置似乎覆盖客户端设置。这里是我的设置:

The problem is that Output Cache settings seem to override the Client settings. Here is what I have setup:

自定义ActionFilterAttribute类

public class ClientCacheAttribute : ActionFilterAttribute
{
    private bool _noClientCache;

    public int ExpireMinutes { get; set; }

    public ClientCacheAttribute(bool noClientCache) 
    {
        _noClientCache = noClientCache;
    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        if (_noClientCache || ExpireMinutes <= 0)
        {
            filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
            filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            filterContext.HttpContext.Response.Cache.SetNoStore();
        }
        else
        {
            filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(ExpireMinutes));
        }

        base.OnResultExecuting(filterContext);
    }
}

Web配置设置

  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="Cache24Hours" location="Server" duration="86400" varyByParam="none" />
    </outputCacheProfiles>
  </outputCacheSettings>

如何我打电话吧:

How I'm calling it:

[OutputCache(CacheProfile = "Cache24Hours")]
[ClientCacheAttribute(false, ExpireMinutes = 10)]
public class HomeController : Controller
{
  [...]
}

但看HTTP头显示:

But looking at the HTTP Header shows:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: text/html; charset=utf-8
Expires: -1

我怎样才能实现这个正常吗?这是一个ASP.NET MVC应用4。

How can I implement this properly? It is an ASP.NET MVC 4 application.

推荐答案

您需要实现服务器端缓存和客户端缓存或者使用ClientCacheAttribute或的OutputCache自己的解决方案。这里有,你为什么会需要服务器端缓存您的自定义解决方案的原因。

You need to implement your own solution for server side caching and for client side caching either use ClientCacheAttribute or OutputCache. Here are the reason for why you would require your custom solution for server side cache.


  • ClientCacheAttribute 设置缓存策略,以Response.Cache这是HttpCachePolicyBase类型

  • 和内置的的OutputCache 还设置缓存策略,以Response.Cache

  • ClientCacheAttribute sets cache policy to Response.Cache which is type of HttpCachePolicyBase
  • and built-in OutputCache also sets cache policy to Response.Cache

下面我想要强调的是,我们没有HttpCachePolicyBase的集合,但我们只有HttpCachePolicyBase的目的之一,所以我们不能为给定响应多个高速缓存策略。

Here what I'm trying to highlight is that we don't have collection of HttpCachePolicyBase but we only have one object of HttpCachePolicyBase so we can't set multiple cache policy for given response.

即使我们可以设置HTTP缓存能力为HttpCacheability.ServerAndPrivate但同样你会在其他问题与缓存持续运行(即客户端10分钟&安培;服务器24小时)

Even if we can set Http Cacheability to HttpCacheability.ServerAndPrivate but again you will run in other issue with cache duration (i.e. for client 10 minute & server 24 hours)

我的建议是,使用的OutputCache 作为客户端缓存和执行服务器端缓存你自己的缓存机制。

What I would suggest is that use OutputCache for client side caching and implement your own caching mechanism for server side caching.

这篇关于如何设置不同的缓存到期时间为客户端和服务器缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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