ASP.NET 服务器端缓存生命周期?
我编写了一个小型 ASHX 处理程序,它使用一个小型状态对象,我希望在一系列请求的生命周期中保留该对象。我让处理程序将对象放入服务器端缓存 (HttpContext.Current.Cache
) 中,并在 ProcessRequest
开始时检索它。
我期望该对象在缓存中保留多长时间?我希望处理程序实例来来去去,所以我希望某些东西能够在所有实例中持久存在(直到请求本身确定不再需要)。但是,如果我让处理程序在由于不在缓存中而必须创建新的状态对象时将其写入应用程序日志,我会看到它创建了 2-3 次。
I have written a small ASHX handler that uses a small state object that I'd like to persist through the lifetime of a series of requests. I have the handler putting the object in the server-side cache (HttpContext.Current.Cache
) and retrieving it at the start of ProcessRequest
.
How long can I expect that object to remain in the cache? I expect handler instances to come and go, so I wanted something to persist across all of them (until no longer needed as determined by the requests themselves). However, if I have the handler write to the application log when it has to create a new state object due to it not being in the cache, I'm seeing it create it 2-3 times.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在添加时指定生命周期和优先级将项目添加到缓存。
不保证项目在整个请求的生命周期内保留在缓存中。例如,如果存在内存压力,则可能会清除缓存,但为项目设置更高的优先级会使其更有可能保留在缓存中。
You can specify the lifetime and priority when you add the item to the cache.
An item isn't guaranteed to stay in the cache for the entire requested lifetime. For example, if there's memory pressure then the cache might be purged, but setting a higher priority for your item makes it more likely that it will remain in the cache.