【发布时间】:2013-07-09 20:10:57
【问题描述】:
给定通用处理程序:
<%@ WebHandler Language="C#" Class="autocomp" %>
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
public class autocomp : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "application/json";
context.Response.BufferOutput = true;
var searchTerm = (context.Request.QueryString["name_startsWith"] + "").Trim();
context.Response.Write(searchTerm);
context.Response.Write(DateTime.Now.ToString("s"));
context.Response.Flush();
}
public bool IsReusable {
get {
return false;
}
}
}
我将如何 server side 根据 name_startsWith 查询字符串参数将此文件缓存 1 小时?使用网络用户控件很容易:
<%@ OutputCache Duration="120" VaryByParam="paramName" %>
但我一直在寻找一段时间来对通用处理程序 (ashx) 文件执行相同操作,但找不到任何解决方案。
【问题讨论】:
-
您将在客户端使用this stuff 进行缓存,然后在您要获取数据时使用 HttpContext.Cache 对实际数据使用服务器端缓存。但是您将无法为此使用任何类型的输出缓存。另外,请记住确保您的 HttpContext.Cache 代码是线程安全的。 ;)
-
在下面编辑了我的答案以回答对帖子的更改。