如何在 .ashx 处理程序上使用输出缓存
如何通过 .ashx 处理程序使用输出缓存? 在本例中,我正在进行一些繁重的图像处理,并希望将处理程序缓存一分钟左右。
另外,有人对如何防止狗堆有任何建议吗?
How can I use output caching with a .ashx handler? In this case I'm doing some heavy image processing and would like the handler to be cached for a minute or so.
Also, does anyone have any recommendations on how to prevent dogpiling?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一些很好的来源,但您希望缓存处理服务器端和客户端。
添加 HTTP 标头应该有助于客户端缓存,
这里有一些可以开始使用的响应标头。
您可以花几个小时调整它们,直到获得所需的性能
至于服务器端缓存,这是一个不同的怪物...并且有很多在那里缓存资源...
There are some good sources but you want to cache you processing server side and client-side.
Adding HTTP headers should help in the client side caching
here are some Response headers to get started on..
You can spend hours tweaking them until you get the desired performance
As for server side caching that is a different monster... and there are plenty of caching resources out there...
你可以这样使用
you can use like this
老问题,但答案并没有真正提到服务器端处理。
正如获胜答案一样,我会将其用于
客户端
:以及
服务器端
,因为您使用的是 ashx 而不是网页,我假设您直接将输出写入Context.Response
。在这种情况下,你可以使用类似的东西(在这种情况下,我想保存基于参数“q”的响应,并且我使用滑动窗口过期)
Old, question but the answer didn't really mentioned the server-side handling.
As in the winning answer, I would use this for the
client side
:and for the
server side
, since you are using a ashx instead of a web page, I'm assuming that you are directly writing the output to theContext.Response
.In that case you could use something like this (in this case I want to save the response based on parameter "q", and Im using a sliding window expiration)
我成功地使用了以下内容,并认为值得在这里发布。
手动控制 ASP.NET 页面输出缓存
来自 http://dotnetperls.com/cache-examples-aspnet< /a>
在 Handler.ashx 文件中设置缓存选项
示例代码摘录:
C# 缓存响应 1 小时
I used the following with success and thought it worthwhile to post here .
Manually controlling the ASP.NET page output cache
From http://dotnetperls.com/cache-examples-aspnet
Setting cache options in Handler.ashx files
Sample code excerpted:
C# to cache response for 1 hour
使用 OutputCachedPage 的解决方案工作得很好,但代价是性能,因为您需要实例化从 System.Web.UI.Page 基类派生的对象。
一个简单的解决方案是使用 Response.Cache.SetCacheability,如上面一些答案所建议的。 然而,为了将响应缓存在服务器上(在输出缓存内),需要使用 HttpCacheability.Server,并设置 VaryByParams 或 VaryByHeaders (请注意,使用 VaryByHeaders 时,URL 不能包含查询字符串,因为将跳过缓存)。
这是一个简单的示例(基于 https://support.microsoft.com/en-us/ kb/323290):
提示:您可以在性能计数器“ASP.NET Applications__Total__\Output Cache Total”中监视缓存。
The solution with the OutputCachedPage works fine, however at a price of the performance, since you need to instantiate an object derived from the System.Web.UI.Page base class.
A simple solution would be to use the Response.Cache.SetCacheability, as suggested by some of the above answers. However for the response to be cached at the server (inside Output Cache) one needs to use HttpCacheability.Server, and set a VaryByParams or VaryByHeaders (note that when using VaryByHeaders URL can't contain a query string, since the cache will be skipped).
Here's a simple example (based on https://support.microsoft.com/en-us/kb/323290):
Hint: you monitor the caching in the Performance Counters "ASP.NET Applications__Total__\Output Cache Total".