VaryByCustom的OutputCache不起作用 [英] OutputCache with VaryByCustom not working

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

问题描述

我正在尝试使用OutputCache属性在ASP.NET MVC 4中实现缓存.

I'm trying to implement caching in ASP.NET MVC 4 using the OutputCache attribute.

这是我的控制器动作:

[HttpGet]
[OutputCache(Duration = CACHE_DURATION, VaryByCustom = "$LanguageCode;myParam", Location = OutputCacheLocation.Server)]
public JsonResult MyAction(string myParam)
{
    // this is called also if should be cached!
}

这是Global.asax中的GetVaryByCustomString:

And here is the GetVaryByCustomString in the Global.asax:

public override string GetVaryByCustomString(HttpContext context, string arg)
{
    var pars = arg.Split(';');
    if (pars.Length == 0) return string.Empty;

    var res = new System.Text.StringBuilder();
    foreach (var s in pars)
    {
        switch (s)
        {
            case "$LanguageCode":
                var culture = CultureManager.GetCurrentCulture();
                res.Append(culture.Name);
                break;
            default:
                var par = context.Request[s];
                if (par != null)
                    res.AppendFormat(par);
                break;
        }
    }
    return base.GetVaryByCustomString(context, res.ToString());
}

总是调用此方法并返回正确的值(例如"it123" ).

This method is always called and returns the right value (e.g. "it123").

如果我使用唯一的 myParam 参数调用该操作,则缓存可以正常工作.

If I call the action with the only myParam parameter, the cache works correctly.

http://localhost:1592/MyController/MyAction?myParam=123 // called multiple times always read from cache

问题是,当我使用另一个未包含在 VaryByCustom 字符串中的参数调用该动作时,无论如何都将调用该控制器动作,是否也应将其缓存并 GetVaryByCustomString 返回相同的结果.

The problem is that when I call the action with another parameter, not included in the VaryByCustom string, the controller action is called anyway, also if is should be cached and the GetVaryByCustomString returns the same result.

http://localhost:1592/MyController/MyAction?myParam=123&dummy=asdf // called multiple times with different 'dummy' values always calls the action

有什么主意吗?

推荐答案

首先,您必须更改您的 [OutputCache] 以包括 VaryByParam =" :

First you have to change your [OutputCache] to include VaryByParam="":

[OutputCache(Duration = CACHE_DURATION, VaryByCustom = "$LanguageCode;myParam", VaryByParam = "", Location = OutputCacheLocation.Server)]

默认情况下,其值为"*" (全部).

Becuase by default its value is "*" (All).

然后在您的 GetVaryByCustomString()方法中,尝试返回生成的字符串,而不是调用基本方法:

Then in your GetVaryByCustomString() method, try to return your generated string instead of calling the base method:

return res.ToString();

这是 base.GetVaryByCustomString()方法的源代码:

public virtual string GetVaryByCustomString(HttpContext context, string custom) {

        if (StringUtil.EqualsIgnoreCase(custom, "browser")) {
            return context.Request.Browser.Type;
        }

        return null;
    }

如您所见,它没有按照您的想象做,它仅将浏览器类型与您提供的字符串进行比较,如果不匹配,则返回 null ,而不返回您提供的字符串.

As you can see, it doesn't do what you think it does, it only compares the Browser type with the string you provided and if there's no match it returns null, not the string you provided.

(我怀疑仅更改 [OutputCache] 就足够了,但也尝试更改方法)

(I suspect the [OutputCache] change alone will suffice, but try changing the method as well)

这篇关于VaryByCustom的OutputCache不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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