2

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 ?

Fraz Sundal
  • 10,288
  • 22
  • 81
  • 132

1 Answers1

3

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;
}
heads5150
  • 7,263
  • 3
  • 26
  • 34