2013-02-12 114 views
0

我有一个动作,比如说在本动作中有一个GET参数的/ Foo/Bar, get_cached,他们定义我们是要获取缓存值还是“实时”。ASP.NET MVC 3高速缓存或不高速缓存动作策略

这与下面的代码制作:

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"); 
} 

这段代码没有问题,除了将URL显示为BarCachedBarRealTime,我只能得到酒吧(主要动作名称)。

我试图RedirectToAction更改为完整的方法名是这样的:

return this.BarCached() 

但这禁用缓存功能!

那么,如何使用此方法上的OutputCache定义(BarCached上的OutputCache)来从方法呈现ActionResult代码(从Bar呈现BarCached)?

提前致谢。

回答

1

在asp.net pipeline中,ResolveRequestCache(OutputCache依赖于)在请求通过身份验证之后发生。在你上面的例子中,到了“Bar”的时候,使用输出缓存已经太晚了,正如你所说的,this.BarCached()不能识别缓存属性。

如果你的问题是无论什么产生"mystuff_"的表现,你能不能只保存调用应用程序缓存的结果,并在你的Bar()方法而不是RedirectToAction对象返回呢?

没有多少我知道的解决方案,但希望有同样的帮助。

+0

没错获得价值,你清楚我的问题。 – eka808 2013-02-13 13:18:14

0

我结束使用System.Web.Caching命名空间谁是asp.net MVC的基本缓存处理程序。 我可以System.Web.HttpContext.Current.Cache

使用访问Asp.NET MVC的高速缓存储存库,我店“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")