如何将输出缓存与 .ashx 处理程序一起使用?在这种情况下,我正在做一些繁重的图像处理,并希望将处理程序缓存一分钟左右。
另外,有没有人对如何防止打狗有任何建议?
有一些很好的来源,但您想缓存处理服务器端和客户端。
添加 HTTP 标头应该有助于客户端缓存
这是一些开始使用的响应标头..
您可以花费数小时来调整它们,直到获得所需的性能
//Adds document content type
context.Response.ContentType = currentDocument.MimeType;
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
context.Response.Cache.SetMaxAge(new TimeSpan(0,10,0));
context.Response.AddHeader("Last-Modified", currentDocument.LastUpdated.ToLongDateString());
// Send back the file content
context.Response.BinaryWrite(currentDocument.Document);
至于服务器端缓存,那是一个不同的怪物......而且那里有很多缓存资源......
你可以这样使用
public class CacheHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
OutputCachedPage page = new OutputCachedPage(new OutputCacheParameters
{
Duration = 60,
Location = OutputCacheLocation.Server,
VaryByParam = "v"
});
page.ProcessRequest(HttpContext.Current);
context.Response.Write(DateTime.Now);
}
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()
{
// when you put the <%@ OutputCache %> directive on a page, the generated code calls InitOutputCache() from here
base.FrameworkInitialize();
InitOutputCache(_cacheSettings);
}
}
}
老问题,但答案并没有真正提到服务器端处理。
就像在获胜的答案中一样,我会将其用于client side
:
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(10));
对于. server side
_Context.Response
在这种情况下,您可以使用类似的东西(在这种情况下,我想根据参数“q”保存响应,并且我使用滑动窗口到期)
using System.Web.Caching;
public void ProcessRequest(HttpContext context)
{
string query = context.Request["q"];
if (context.Cache[query] != null)
{
//server side caching using asp.net caching
context.Response.Write(context.Cache[query]);
return;
}
string response = GetResponse(query);
context.Cache.Insert(query, response, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10));
context.Response.Write(response);
}
我成功地使用了以下内容,并认为值得在这里发布。
来自http://dotnetperls.com/cache-examples-aspnet
在 Handler.ashx 文件中设置缓存选项
首先,您可以在 ASP.NET 中使用 HTTP 处理程序,以比 Web 表单页面更快地提供服务器动态内容。Handler.ashx 是 ASP.NET 通用处理程序的默认名称。您需要使用 HttpContext 参数并以这种方式访问 Response 。
示例代码摘录:
<%@ WebHandler Language="C#" Class="Handler" %>
C# 缓存响应 1 小时
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
// Cache this handler response for 1 hour.
HttpCachePolicy c = context.Response.Cache;
c.SetCacheability(HttpCacheability.Public);
c.SetMaxAge(new TimeSpan(1, 0, 0));
}
public bool IsReusable {
get {
return false;
}
}
}
使用OutputCachedPage的解决方案工作正常,但是以性能为代价,因为您需要实例化从System.Web.UI.Page基类派生的对象。
正如上述一些答案所建议的那样,一个简单的解决方案是使用Response.Cache.SetCacheability 。但是,要在服务器(在输出缓存内)缓存响应,需要使用HttpCacheability.Server,并设置VaryByParams或VaryByHeaders(请注意,使用VaryByHeaders时, URL 不能包含查询字符串,因为缓存将被跳过)。
这是一个简单的示例(基于https://support.microsoft.com/en-us/kb/323290):
<%@ WebHandler Language="C#" Class="cacheTest" %>
using System;
using System.Web;
using System.Web.UI;
public class cacheTest : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
TimeSpan freshness = new TimeSpan(0, 0, 0, 10);
DateTime now = DateTime.Now;
HttpCachePolicy cachePolicy = context.Response.Cache;
cachePolicy.SetCacheability(HttpCacheability.Public);
cachePolicy.SetExpires(now.Add(freshness));
cachePolicy.SetMaxAge(freshness);
cachePolicy.SetValidUntilExpires(true);
cachePolicy.VaryByParams["id"] = true;
context.Response.ContentType = "application/json";
context.Response.BufferOutput = true;
context.Response.Write(context.Request.QueryString["id"]+"\n");
context.Response.Write(DateTime.Now.ToString("s"));
}
public bool IsReusable
{
get
{
return false;
}
}
}
提示:您在性能计数器“ASP.NET Applications__Total__\Output Cache Total”中监控缓存。