使用缓存配置文件缓存 ChildActions 不起作用? [英] Caching ChildActions using cache profiles won't work?

查看:29
本文介绍了使用缓存配置文件缓存 ChildActions 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用缓存配置文件在我的 mvc 应用程序中缓存子操作,但出现异常:持续时间必须为正数.

I'm trying to use cache profiles for caching child actions in my mvc application, but I get an exception: Duration must be a positive number.

我的 web.config 如下所示:

My web.config looks like this:

<caching>
      <outputCache enableOutputCache="true" />
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="TopCategories" duration="3600" enabled="true" varyByParam="none" />
        </outputCacheProfiles>
      </outputCacheSettings>
</caching>

我孩子的动作是这样的:

And my child action something like this:

[ChildActionOnly]
[OutputCache(CacheProfile = "TopCategories")]
//[OutputCache(Duration = 60)]
public PartialViewResult TopCategories()
{
    //...
    return PartialView();
}

在视图中,我只是调用了 @Html.RenderAction("TopCategories", "Category")

Inside a view I just call @Html.RenderAction("TopCategories", "Category")

但我收到一个错误:异常详细信息:System.InvalidOperationException:持续时间必须是正数.

But I get an error: Exception Details: System.InvalidOperationException: Duration must be a positive number.

如果我不使用缓存配置文件,它就可以工作.知道有什么问题吗?

If I don't use cache profile it works. Have an idea what's the problem?

推荐答案

我对 相关问题 并查看 mvc 3 源代码,它们绝对不支持 Duration 以外的任何属性和 VaryByParam.他们当前实现的主要错误是,如果您不提供其中任何一个,您将收到一个异常告诉您提供它,而不是一个异常表示您尝试使用的内容不受支持.另一个主要问题是,即使您在 web.config 中关闭缓存,它们也会缓存,这看起来非常蹩脚且不对.

I did some digging on a related question and looking at mvc 3 source, they definitely don't support any attribute other than Duration and VaryByParam. The main bug with their current implementation is that if you don't supply either one of these you will get an exception telling you to supply that, instead of an exception say that what you tried to use is not supported. The other major issue was that they will cache even if you turn off caching in the web.config, which seems really lame and not right.

我遇到的最大问题是它们使用了在视图和局部视图中都有效的相同属性,但实际上它可能应该是 2 个不同的属性,因为局部视图非常有限并且行为有很大不同,至少在目前的实现中是这样.

The biggest issue I had with it all is that they are using the same attribute which works in both views and partial views, but in reality it should probably be 2 different attributes since the partial view is so limited and behaves a lot differently, at least in it's current implementation.

这篇关于使用缓存配置文件缓存 ChildActions 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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