• ashx页面缓存


    当用户访问页面时,整个页面将会被服务器保存在内存中,这样就对页面进行了缓存。当用户再次访问该页,页面不会再次执行数据操作,页面首先会检查服务器中是否存在缓存,如果缓存存在,则直接从缓存中获取页面信息,如果页面不存在,则创建缓存。

    页面输出缓存适用于那些数据量较多,而不会进行过多的事件操作的页面,如果一个页面需要执行大量的事件更新,以及数据更新,则并不能使用页面输出缓存。

    通常我们在缓存aspx页面的时候,可以页面通过如下设置

    <%@ OutputCache Duration="120" VaryByParam="paramName" %>

    上述代码使用@OutputCatch指令声明了页面缓存,该页面将被缓存120秒。@OutputCatch指令包括10个属性,通过这些属性能够分别为页面的不同情况进行缓存设置,常用的属性如下所示:

     CacheProfile:获取或设置OutputCacheProfile名称。

     Duration:获取或设置缓存项需要保留在缓存中的时间。

     VaryByHeader:获取或设置用于改变缓存项的一组都好分隔的HTTP标头名称。

     Location:获取或设置一个值,该值确定缓存项的位置,包括Any、Clint、Downstream、None、Server和ServerAndClient。默认值为Any。

     VaryByControl:获取或设置一簇分好分隔的控件标识符,这些标识符包含在当前页或用户控件内,用于改变当前的缓存项。

     NoStore:获取或设置一个值,该值确定是否设置了“Http Cache-Control:no-store”指令。

     VaryByCustom:获取输出缓存用来改变缓存项的自定义字符串列表。

     Enabled:获取或设置一个值,该值指示是否对当前内容启用了输出缓存。

     VaryByParam:获取查询字符串或窗体POST参数的列表。

    对于aspx页面来说,非常简单。那么在ashx页面中,我们又该如何设页面缓存呢?

    直接上代码,如下:

    public class autocomp : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            OutputCachedPage page = new OutputCachedPage(new OutputCacheParameters
            {
                Duration = 120,
                Location = OutputCacheLocation.Any,
                VaryByParam = "paramName"
            });
    
            page.ProcessRequest(HttpContext.Current);
    
            context.Response.ContentType = "application/json";
            context.Response.BufferOutput = true;
    
            var searchTerm = (context.Request.QueryString["paramName"] + "").Trim();
    
            context.Response.Write(searchTerm);
            context.Response.Write(DateTime.Now.ToString("s"));
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private sealed class OutputCachedPage : Page
        {
            private OutputCacheParameters _cacheSettings;
    
            public OutputCachedPage(OutputCacheParameters cacheSettings)
            {
                // Tracing requires Page IDs to be unique.
                ID = Guid.NewGuid().ToString();
                _cacheSettings = cacheSettings;
            }
    
            protected override void FrameworkInitialize()
            {
                base.FrameworkInitialize();
                InitOutputCache(_cacheSettings);
            }
        }
    }

    如果Query String多个查询参数,注意查看VaryByParam
    public class test : IHttpHandler
    {
    
        public void ProcessRequest(HttpContext context)
        {
            OutputCachedPage page = new OutputCachedPage(new OutputCacheParameters
            {
                Duration = 120,
                Location = OutputCacheLocation.Server,
                VaryByParam = "name;city"
            });
    
            page.ProcessRequest(HttpContext.Current);
    
            context.Response.ContentType = "application/json";
            context.Response.BufferOutput = true;
    
            var searchTerm = (context.Request.QueryString["name"] + "").Trim();
            var searchTerm2 = (context.Request.QueryString["city"] + "").Trim();
    
            context.Response.Write(searchTerm+" "+searchTerm2+" ");
            context.Response.Write(DateTime.Now.ToString("s"));
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private sealed class OutputCachedPage : Page
        {
            private OutputCacheParameters _cacheSettings;
    
            public OutputCachedPage(OutputCacheParameters cacheSettings)
            {
                // Tracing requires Page IDs to be unique.
                ID = Guid.NewGuid().ToString();
                _cacheSettings = cacheSettings;
            }
    
            protected override void FrameworkInitialize()
            {
                base.FrameworkInitialize();
                InitOutputCache(_cacheSettings);
            }
        }
    }
    
    
    
     
  • 相关阅读:
    华为数据之道03
    机器学习10讲 第二讲
    机器学习10讲 第一讲
    07.TensorFlow双向循环神经网络
    06.TensorFlow循环神经网络
    华为数据之道:华为数据治理2
    线性回归
    MySQL配置文件my.cnf的理解
    Linux怎么查看软件安装路径 查看mysql安装在哪
    hadoop-2.7.4-nodemanager无法启动问题解决方案
  • 原文地址:https://www.cnblogs.com/event/p/4778385.html
Copyright © 2020-2023  润新知