控制 ASP.NET 输出缓存内存使用

发布于 2024-09-02 02:12:26 字数 1266 浏览 7 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动次打次papapa 2024-09-09 02:12:26

啊!我现在感觉很愚蠢...看来我可以在 IIS 的配置中设置此限制,这是有道理的,因为我猜 IIS 首先向 ASP.NET 提供输出缓存服务。

作为一个额外的好处,IIS 似乎已经为此默认了一些合理的设置:

最大缓存响应大小(以字节为单位)指定
两者的缓存响应
用户模式和内核模式缓存。这
默认值为 262144 字节。这
字段在服务器级别启用
仅有的;它在所有其他地方都是只读的
水平。

缓存大小限制(以 MB 为单位) 配置缓存大小限制
用户模式和内核模式缓存。你
可以键入大小(以 MB 为单位)或键入 0。如果
您输入 0,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:

Maximum cached response size (in bytes) Specifies the maximum size of
a cached response for both the
user-mode and kernel-mode caches. The
default value is 262144 bytes. This
field is enabled at the server level
only; it is read-only at all other
levels.

Cache size limit (in MB) Configures the size limit of both the
user-mode and kernel-mode caches. You
can type a size (in MB) or type 0. If
you type 0, IIS uses half of the
available physical memory or virtual
memory, whichever is less. This field
is enabled at the server level only;
it is read-only at all other levels.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文