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);
        }
    }
}
复制代码

 

 
复制代码

 

posted @   乡下伢  阅读(897)  评论(0编辑  收藏  举报
编辑推荐:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示