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

后端 未结 3 1062
终归单人心
终归单人心 2021-02-07 16:45

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 w

相关标签:
3条回答
  • 2021-02-07 17:11

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

    0 讨论(0)
  • 2021-02-07 17:16

    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.

    0 讨论(0)
  • 2021-02-07 17:19

    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.

    0 讨论(0)
提交回复
热议问题