以编程方式控制输出缓存-根据参数值禁用或启用缓存 [英] programmatically control output caching - disable or enable cache according to parameter value

查看:74
本文介绍了以编程方式控制输出缓存-根据参数值禁用或启用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个相当标准的电子商务场景,其中包含类别内产品的分页列表.不管是好是坏,大约80%的访问者从未浏览过第一页,然后根据类别,可能会多出5-10页的结果,而浏览的频率则要低得多. (是的,我们确实优化了首页上显示的内容并进行了很好的搜索-但这是不同的讨论)

We've got a fairly standard e-commerce scenario with paged lists of products within categories. For better or worse, about 80% of visitors never navigate past the first page, depending on the category there may then be 5-10 more pages of results which are viewed far less often. (Yes we do optimise what appears on the first page and have good search - but that's a different discussion)

由于受内存的限制,我们无法缓存每一页结果,但是仅缓存每个类别的第一页结果的好处将是巨大的.

We can't cache every single page of results, because we're constrained by memory, but the benefit of caching just the first page of results for each category would be huge.

我知道我可以使用对象缓存来存储相关数据集,但通过输出缓存(也许通过使用response.Cache对象)可以做到这一点吗?

I know I could do something similar using object caching to store the datasets in question, but is this possible using output caching, perhaps by using the response.Cache object?

在页面生命周期的何处可以完成?预渲染?

Where in the page lifecycle could this be done? Pre-render?

经过简化,URL类似于"/ProductList?Category = something& Page = 1",而我想要类似(伪代码)的逻辑:

Much simplified, the URL is something like "/ProductList?Category=something&Page=1" And I'd want logic something like (pseudocode):

If paramater "Page" equals 1
   Use output caching: vary by param = "categoryName; page"
else
   Don't use caching at all, just render the page from scratch.

我们在IIS 6/win2003上使用ASP.NET 2.0.

We're using ASP.NET 2.0, on IIS 6/win2003.

推荐答案

除了使用OutputCache指令外,还可以通过编程方式执行相同的操作,如下所示:

Instead of using the OutputCache directive, you can do the same thing programmatically, as follows:

if (yourArbitraryCondition) {
  OutputCacheParameters outputCacheSettings = new OutputCacheParameters();
  outputCacheSettings.Duration = 60;
  InitOutputCache(outputCacheSettings);
}

从OnInit进行此操作应该可以正常工作.显然,您可以通过在OutputCacheParameter上设置各种属性来调整缓存行为,该属性具有与指令相同的旋钮(实际上,这是我们在使用指令时生成的).

Doing this from OnInit should work fine. And obviously, you can tweak the caching behavior by setting the various properties on the OutputCacheParameter, which has all the same knobs as the directive (in fact, that's what we generate when you use the directive).

关键是您仅在有条件的情况下执行此逻辑,而指令使它成为无条件的.

The key point is that you're only executing this logic conditionally, while the directive makes it unconditional.

更新:

作为替代方案,您可以使用上面的代码所基于的低级缓存API.例如

As an alternative, you can use the low level cache API that the code above is built on. e.g.

HttpCachePolicy cache = Response.Cache;
cache.SetCacheability(HttpCacheability.Public);
cache.SetExpires(Context.Timestamp.AddSeconds(60));
cache.VaryByParams["categoryName"] = true;

基本上,这是做同一件事的另一种方式,而不使用任何标记为不应调用"的API.最后,任何一种方法都行得通,所以请选择.

Basically, it's another way of doing the same thing, without using any API's marked as 'should not be called'. In the end, either way will work, so take your pick.

这篇关于以编程方式控制输出缓存-根据参数值禁用或启用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆