如何在 asp.net mvc 2 的辅助方法中应用缓存

发布于 2024-12-22 10:15:26 字数 264 浏览 4 评论 0原文

我的应用程序中有一个辅助方法,并且我已对其应用了输出缓存,

[OutputCache(Duration = 3600, VaryByParam = "DetailsId")]
public static Dictionary<string, object> GetData(int DetailsId)
{

}

但在每次请求时都会调用此函数。

我想知道我可以在 Helper 方法上应用输出缓存吗?如果是的话怎么办?

I have a helper method in my application and i have applied output caching on it

[OutputCache(Duration = 3600, VaryByParam = "DetailsId")]
public static Dictionary<string, object> GetData(int DetailsId)
{

}

but on every request this function is called.

I want to know can i apply Output Cache on Helper method? If yes then how ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吖咩 2024-12-29 10:15:26

您的输出缓存属性需要位于 ActionResult 上,而不是位于静态或非静态方法上。

例如,

[OutputCache(Duration = 3600, VaryByParam = "DetailsId")]
public ViewResult GetData(int DetailsId)
{

}

简而言之,您不能使用此级别的 OutputCache 属性,请使用与缓存对象类似的内容:

public Dictionary<string,object> GetData(int DetailsId)
{
 //Try to get object from cache
var model = (Dictionary<string,object>)HttpContextBase.Current.Cache["Data_"+DetailsId];
if(model==null)
{
HttpContextBase.Current.Cache["Data_"+DetailsId] = model_from_store;
return model_from_store;
}
else
return model;
}

Your Output Cache attribute needs to be on an ActionResult not on a static or non static method.

For instance

[OutputCache(Duration = 3600, VaryByParam = "DetailsId")]
public ViewResult GetData(int DetailsId)
{

}

In short you cannot use the OutputCache attribute this level use something along the lines of the Cache Object:

public Dictionary<string,object> GetData(int DetailsId)
{
 //Try to get object from cache
var model = (Dictionary<string,object>)HttpContextBase.Current.Cache["Data_"+DetailsId];
if(model==null)
{
HttpContextBase.Current.Cache["Data_"+DetailsId] = model_from_store;
return model_from_store;
}
else
return model;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文