zoukankan      html  css  js  c++  java
  • ASP.NET MVC Action Filter Caching and Compression

    Caching plays a major role in developing highly scalable web applications. We can cache any http get request in the user browser for a predefined time, if the user request the same URL in that predefined time the response will be loaded from the browser cache instead of the server. You can archive the same in ASP.NET MVC application with the following action filter:

    using System;
    using System.Web;
    using System.Web.Mvc;

    public class CacheFilterAttribute : ActionFilterAttribute
    {
    /// <summary>
    /// Gets or sets the cache duration in seconds. The default is 10 seconds.
    /// </summary>
    /// <value>The cache duration in seconds.</value>
    public int Duration
    {
    get;
    set;
    }

    public CacheFilterAttribute()
    {
    Duration = 10;
    }

    public override void OnActionExecuted(FilterExecutedContext filterContext)
    {
    if (Duration <= 0) return;

    HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
    TimeSpan cacheDuration = TimeSpan.FromSeconds(Duration);

    cache.SetCacheability(HttpCacheability.Public);
    cache.SetExpires(DateTime.Now.Add(cacheDuration));
    cache.SetMaxAge(cacheDuration);
    cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
    }
    }

    You can apply the filter in your Controller action method like the following.

    [CacheFilter(Duration = 60)]
    public void Category(string name, int? page)

    The following shows the screen-shot in firebug  when cache filter is not applied:

    and this is the screen-shot when the cache filter is applied:

    Another important thing is compression. Now a days, all modern browsers accept compressed contents and it saves huge bandwidth. You can apply the following action filter to compress your response in your  ASP.NET MVC application:

    using System.Web;
    using System.Web.Mvc;

    public class CompressFilter : ActionFilterAttribute
    {
    public override void OnActionExecuting(FilterExecutingContext filterContext)
    {
    HttpRequestBase request = filterContext.HttpContext.Request;

    string acceptEncoding = request.Headers["Accept-Encoding"];

    if (string.IsNullOrEmpty(acceptEncoding)) return;

    acceptEncoding = acceptEncoding.ToUpperInvariant();

    HttpResponseBase response = filterContext.HttpContext.Response;

    if (acceptEncoding.Contains("GZIP"))
    {
    response.AppendHeader("Content-encoding", "gzip");
    response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
    }
    else if (acceptEncoding.Contains("DEFLATE"))
    {
    response.AppendHeader("Content-encoding", "deflate");
    response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
    }
    }
    }

    Just decorate your controller action with this filter:

    [CompressFilter]
    public void Category(string name, int? page)

    The following shows when compression is not applied:

    and this is the screen-shot when the compress filter is applied:

    You can also apply both these filter in the same action method, like the following:

    [CompressFilter(Order = 1)]
    [CacheFilter(Duration = 60, Order = 2)]
    public void Category(string name, int? page)

    And this is the screen-shot:


    Enjoy!!!( From:http://weblogs.asp.net/rashid/archive/2008/03/28/asp-net-mvc-action-filter-caching-and-compression.aspx)

  • 相关阅读:
    Qt开发技术:QCharts(二)QCharts折线图介绍、Demo以及代码详解
    OpenCV开发笔记(六十八):红胖子8分钟带你使用特征点Flann最邻近差值匹配识别(图文并茂+浅显易懂+程序源码)
    keepalived+MySQL实现高可用
    使用ProxySQL实现MySQL Group Replication的故障转移、读写分离(二)
    使用ProxySQL实现MySQL Group Replication的故障转移、读写分离(一)
    Oracle Dataguard故障转移(failover)操作
    Oracle DataGuard故障转移(failover)后使用RMAN还原失败的主库
    MySQL组复制MGR(四)-- 单主模式与多主模式
    MySQL组复制MGR(三)-- 组复制监控
    MySQL组复制MGR(二)-- 组复制搭建
  • 原文地址:https://www.cnblogs.com/guanjie20/p/1775869.html
Copyright © 2011-2022 走看看