缓存在我的ASP.NET MVC网站中无法正常工作?

时间:2022-02-04 03:33:14

i'm trying to use OutputCaching in my ASP.NET MVC website. Problem is, when i try and change the value of one my querystring params, it's returning the data for the first item that was requested!

我正在尝试在我的ASP.NET MVC网站中使用OutputCaching。问题是,当我尝试更改一个我的查询字符串参数的值时,它将返回所请求的第一个项目的数据!

Here's my code (with the param names changed) ...

这是我的代码(更改了param名称)......

[ApiAuthorize]
[HandleErrorAsJson]
public class SearchController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    [OutputCache(Duration = 60, VaryByParam = "*")]
    public ActionResult ScoreCard(string foo, byte? bar, byte? pewpew)
    {
    ..
    }
}
  • NOTE 1: ApiAuthorize - custom attribute that checks for a querystring param called 'Key' and checks an in memory dictionary, to see if it exists.
  • 注1:ApiAuthorize - 自定义属性,用于检查名为“Key”的查询字符串参数,并检查内存字典,以查看它是否存在。

  • NOTE 2: HandleErrorAsJson - custom attribute that returns the error message as json if an exception was/is thrown.
  • 注2:HandleErrorAsJson - 自定义属性,如果抛出异常,则返回错误消息为json。

and here's two sample calls i'm making to this action :-

这是我正在做这个动作的两个示例调用: -

so the data from the first call (foo = hello world, Pew Pew) is returned as a 200 OK. Then the second api call returns a 200 OK but with the data from the previous call.

所以第一次调用的数据(foo = hello world,Pew Pew)返回200 OK。然后第二个api调用返回200 OK但使用前一个调用的数据。

Also, i'm not using any proxy server. If i comment out the OutputCache attribute, all is good.

此外,我没有使用任何代理服务器。如果我注释掉OutputCache属性,一切都很好。

I've also tried the following (manually listing each time i need to cache) .....

我也尝试了以下(每次我需要缓存时手动列出).....

[OutputCache(Duration = 60, VaryByParam = "foo,key,bar,pewpew")]

No luck :(

没运气 :(

Notice how i need to make sure that i include the API 'Key' parameter as part of the cache unique key. I don't want to people to search for the same thing, but if the second person doesn't have the right key, they shouldn't get a cached result, but an error message (techinically, it's a 401 Not Authorised, but anyways)...

请注意我需要确保将“Key”参数作为缓存唯一键的一部分包含在内。我不希望人们搜索相同的东西,但如果第二个人没有正确的密钥,他们不应该得到缓存的结果,而是一个错误信息(技术上,它是401未授权,但无论如何)...

Thoughts?

2 个解决方案

#1


4  

The list of parameters in VaryByParam is supposed to be semicolon delimited, not comma delimited. Give that a try. That said, the * should have worked.

VaryByParam中的参数列表应该是以分号分隔的,而不是逗号分隔的。试一试。那就是说,*应该有效。

#2


0  

I use a filter for output caching which will give programmatic control for debugging and also will set when it needs to be. Feel free to use this and see if setting it here would make the difference which I think it might (also make sure any previous caching is cleared)

我使用过滤器进行输出缓存,它将为调试提供编程控制,并在需要时设置。随意使用它,看看在这里设置是否会产生我认为可能的差异(也确保任何以前的缓存被清除)

public class CacheFilterAttribute : ActionFilterAttribute
    {
        private const int Second = 1;
        private const int Minute = 60 * Second;
        private const int Hour = 60 * Minute;
        public const int SecondsInDay = Hour * 24;


        /// <summary>
        /// Gets or sets the cache duration in seconds. The default is 10 seconds.
        /// </summary>
        /// <value>The cache duration in seconds.</value>
        public int Duration
        {
            get;
            set;
        }

        public int DurationInDays
        {
            get { return Duration / SecondsInDay; }
            set { Duration = value * SecondsInDay; }
        }

        public CacheFilterAttribute()
        {
            Duration = 10;
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (Duration <= 0) return;

            HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
            TimeSpan cacheDuration = TimeSpan.FromSeconds(Duration);

            cache.SetCacheability(HttpCacheability.Public);
            cache.SetExpires(DateTime.Now.Add(cacheDuration));
            cache.SetMaxAge(cacheDuration);
            cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
        }
    }

I'm just using this so far for a very static part of a site, so have a call like this

到目前为止,我只是将它用于网站的一个非常静态的部分,所以请拨打这样的电话

[CacheFilter(DurationInDays = 1)]

Obviously you'd just like to extend this to have VaryByParams exposed as a property, can provide but sounds like you'd know what to do.

显然你只是想扩展它以使VaryByParams作为属性公开,可以提供但听起来像你知道该怎么做。

#1


4  

The list of parameters in VaryByParam is supposed to be semicolon delimited, not comma delimited. Give that a try. That said, the * should have worked.

VaryByParam中的参数列表应该是以分号分隔的,而不是逗号分隔的。试一试。那就是说,*应该有效。

#2


0  

I use a filter for output caching which will give programmatic control for debugging and also will set when it needs to be. Feel free to use this and see if setting it here would make the difference which I think it might (also make sure any previous caching is cleared)

我使用过滤器进行输出缓存,它将为调试提供编程控制,并在需要时设置。随意使用它,看看在这里设置是否会产生我认为可能的差异(也确保任何以前的缓存被清除)

public class CacheFilterAttribute : ActionFilterAttribute
    {
        private const int Second = 1;
        private const int Minute = 60 * Second;
        private const int Hour = 60 * Minute;
        public const int SecondsInDay = Hour * 24;


        /// <summary>
        /// Gets or sets the cache duration in seconds. The default is 10 seconds.
        /// </summary>
        /// <value>The cache duration in seconds.</value>
        public int Duration
        {
            get;
            set;
        }

        public int DurationInDays
        {
            get { return Duration / SecondsInDay; }
            set { Duration = value * SecondsInDay; }
        }

        public CacheFilterAttribute()
        {
            Duration = 10;
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (Duration <= 0) return;

            HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
            TimeSpan cacheDuration = TimeSpan.FromSeconds(Duration);

            cache.SetCacheability(HttpCacheability.Public);
            cache.SetExpires(DateTime.Now.Add(cacheDuration));
            cache.SetMaxAge(cacheDuration);
            cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
        }
    }

I'm just using this so far for a very static part of a site, so have a call like this

到目前为止,我只是将它用于网站的一个非常静态的部分,所以请拨打这样的电话

[CacheFilter(DurationInDays = 1)]

Obviously you'd just like to extend this to have VaryByParams exposed as a property, can provide but sounds like you'd know what to do.

显然你只是想扩展它以使VaryByParams作为属性公开,可以提供但听起来像你知道该怎么做。