0

I have an action, let's say /Foo/Bar with a GET parameter in this action, get_cached, who define if we want to get the cached value or the "realtime".

This is made with the following code :

public ActionResult Bar()
{
    var useCache = Request.Params["get_cached"] == "1" ? true : false;

    if (useCache)
    {
        return RedirectToAction("BarCached");
    }
    else
    {
        return RedirectToAction("BarRealTime");
    }
}

[OutputCache(Duration = 100, VaryByParam = "*")]
public ActionResult BarCached()
{
    return Content("mystuff_cached");
}
public ActionResult BarRealTime()
{
    return Content("mystuff_realtime");
}

No problem with this code, apart the url will be shown as BarCached or BarRealTime and i would get only Bar (the main action name).

I tried to change the RedirectToAction to the full method name like this :

return this.BarCached()

But this disable the cache capabilities !

So, how can render the ActionResult code from a method (render BarCached from Bar) using the OutputCache definitions on this method (OutputCache on BarCached) ?

Thanks by advance.

4

2 回答 2

1

在 asp.net管道中,ResolveRequestCache(OutputCache 所依赖的)在请求通过身份验证后发生。在上面的示例中,当您到达“Bar”时,使用输出缓存为时已晚,正如您所说的那样,this.BarCached()它无法识别缓存属性。

如果您的问题是任何 generate 的性能"mystuff_",您能否不只是将该调用的结果保存到应用程序缓存并在您的Bar()方法而不是RedirectToAction对象中返回它?

我知道的解决方案不多,但希望同样有用。

于 2013-02-12T18:57:58.323 回答
0

我结束了使用作为System.Web.Cachingasp.net MVC 的基本缓存处理程序的命名空间。我可以访问 Asp.NET MVC 的缓存存储库System.Web.HttpContext.Current.Cache

使用它,我存储“BarCached”的 ActionResult,然后我可以使用类似这样的方式以我想要的方式获取缓存功能:

向缓存中添加值

System.Web.HttpContext.Current.Cache.Insert(
                    "mykey",
                    "myvalue",
                    null,
                    DateTime.Now.AddSeconds(expirationInSeconds),
                    System.Web.Caching.Cache.NoSlidingExpiration
                );

并从缓存中获取价值

var myvalue = System.Web.HttpContext.Current.Cache.Get("mykey")
于 2013-02-13T13:23:17.660 回答