使用 HttpModule 跨请求缓存
我编写了一个 HttpModule,它接受请求、处理请求(通过数据库查找)并将结果 (html) 输出回响应。 (请注意,HttpModule 实际上会在完成后结束请求,因此不会对请求进行正常的 ASP.NET 处理。)
由于数据库查找可能会很昂贵/耗时,我想将结果存储在内存中以便后续(相同)请求可以提供相同的内容,而无需访问数据库。
我可以在哪里存储(内存中)数据,以便后续调用 HttpModule 时可用?
I have written an HttpModule that accepts the request, processes it (via a database lookup), and outputs results (html) back to the response. (Note that the HttpModule actually ends the request after it is done, so there is no normal ASP.NET processing of the request.)
As the database lookup can be expensive/time-consuming, I would like to store the results in-memory so that subsequent (identical) requests can be served the same content without going to the database.
Where can I store (in-memory) data so that it is available for subsequent invocations of the HttpModule?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将其存储在
Application.Cache
中,然后结果将在应用程序范围内可用。 如有必要,请务必时不时检查“新数据”。You could store it in the
Application.Cache
, the result would be available Application wide then. Be sure to check for "new data" every now and then if necessary.与从数据库获取的数据相比,响应大小如何? 一旦内存中有这些数据,渲染怎么样? 如果渲染不是问题,我只需缓存数据并在每个请求上渲染它。 如果渲染需要大量 CPU,那么您应该缓存完整的响应并直接从缓存中提供服务。
How is response size compared to your data fetched from db? What about rendering once you have that data in memory? If rendering is not an issue, I would just cache the data and render it on each request. If rendering takes lots of cpu, then you should cache the full response and directly serve it from cache.