How to set different Cache expire times for Client and Server caches

强颜欢笑 提交于 2020-01-12 04:28:30

问题


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:

Custom ActionFilterAttribute Class

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 Config settings

  <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
{
  [...]
}

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

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


回答1:


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 sets cache policy to Response.Cache which is type of HttpCachePolicyBase
  • and built-in OutputCache also sets cache policy to Response.Cache

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.

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)

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




回答2:


In your OutputCache profile, set the location to ServerAndClient. Your action filter should properly override the output once you do this.




回答3:


Under .Net 4.5, it IS possible to have different client and server caching policies without writing custom cache providers:

// Set the cache response expiration to 3600 seconds (use your own value here).
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddSeconds(3600));

// Set both server and browser caching.
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);

// Prevent browser's default max-age=0 header on first request
// from invalidating the server cache on each request.
HttpContext.Current.Response.Cache.SetValidUntilExpires(true);

// Set an HTTP ETag header on the page using a random GUID.
HttpContext.Current.Response.Cache.SetETag(System.Guid.NewGuid()
                                           .ToString().Replace("-", ""));

// Set last modified time.
HttpContext.Current.Response.Cache.SetLastModified(DateTime.UtcNow);

// Now here is the critical piece that forces revalidation on each request!:
HttpContext.Current.Response.Cache.AppendCacheExtension(
    "must-revalidate, proxy-revalidate, max-age=0");

The result is that each time the page gets regenerated in the server cache, it gets a new ETag. This causes the If-None-Match request to return the full page back to the browser. If the browser's cached copy is the same as what's generated in the server cache (same ETag), the browser gets back a 304 Not Modified.

Note that none of the cache headers I appended there in AppendCacheExtension conflict with the headers emitted by native caching. Whenever you attempt to modify the caching headers emitted by .Net caching itself, .Net will always supersede what you're trying to do. The trick is to add new non-conflicting headers, not try to change what .Net is already emitting. Most importantly, you must append the max-age header. You must NOT use .SetMaxAge(), as this also sets the maximum age of the server cached copy of the page.

This took quite a bit of effort to figure out, but it DOES work, at least in .Net 4.5.



来源:https://stackoverflow.com/questions/14823057/how-to-set-different-cache-expire-times-for-client-and-server-caches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!