控制 ASP.NET 输出缓存内存使用
我想在 WCF 数据服务中使用输出缓存,虽然没有专门内置任何内容来支持缓存,但有一个 OnStartProcessingRequest 方法允许我使用正常的 ASP.NET 机制挂钩并设置请求的可缓存性。
但我担心如果缓存大量响应,工作进程会因内存消耗过多而被回收。有没有办法指定 ASP.NET 输出缓存的上限,以便如果超过此限制,缓存中的项目将被丢弃?
我已经看到缓存配置设置,但我从文档中得到的印象是,这是通过 Cache 对象进行显式缓存,因为有一个单独的 outputCacheSettings 没有与内存相关的属性。
以下是 Scott Hanselman 的帖子 显示了我如何设置请求的可缓存性。
protected override void OnStartProcessingRequest(ProcessRequestArgs args)
{
base.OnStartProcessingRequest(args);
//Cache for a minute based on querystring
HttpContext context = HttpContext.Current;
HttpCachePolicy c = HttpContext.Current.Response.Cache;
c.SetCacheability(HttpCacheability.ServerAndPrivate);
c.SetExpires(HttpContext.Current.Timestamp.AddSeconds(60));
c.VaryByHeaders["Accept"] = true;
c.VaryByHeaders["Accept-Charset"] = true;
c.VaryByHeaders["Accept-Encoding"] = true;
c.VaryByParams["*"] = true;
}
I would like to use output caching with WCF Data Services and although there's nothing specifically built in to support caching, there is an OnStartProcessingRequest method that allows me to hook in and set the cacheability of the request using normal ASP.NET mechanisms.
But I am worried about the worker process getting recycled due to excessive memory consumption if large responses are cached. Is there a way to specify an upper limit for the ASP.NET output cache so that if this limit is exceeded, items in the cache will be discarded?
I've seen the caching configuration settings but I get the impression from the documentation that this is for explicit caching via the Cache object since there is a separate outputCacheSettings which has no memory-related attributes.
Here's a code snippet from Scott Hanselman's post that shows how I'm setting the cacheability of the request.
protected override void OnStartProcessingRequest(ProcessRequestArgs args)
{
base.OnStartProcessingRequest(args);
//Cache for a minute based on querystring
HttpContext context = HttpContext.Current;
HttpCachePolicy c = HttpContext.Current.Response.Cache;
c.SetCacheability(HttpCacheability.ServerAndPrivate);
c.SetExpires(HttpContext.Current.Timestamp.AddSeconds(60));
c.VaryByHeaders["Accept"] = true;
c.VaryByHeaders["Accept-Charset"] = true;
c.VaryByHeaders["Accept-Encoding"] = true;
c.VaryByParams["*"] = true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
啊!我现在感觉很愚蠢...看来我可以在 IIS 的配置中设置此限制,这是有道理的,因为我猜 IIS 首先向 ASP.NET 提供输出缓存服务。
作为一个额外的好处,IIS 似乎已经为此默认了一些合理的设置:
Ahh! I feel dumb now... It appears I can set this limit in IIS's configuration, which makes sense since I guess IIS is providing the output caching services to ASP.NET in the first place.
And as an added bonus it seems IIS has already defaulted to some reasonable settings for this: