这就是我所拥有的:
[OutputCache(Duration = 3600, VaryByParam = "model")]
public object Hrs(ReportFilterModel model) {
var result = GetFromDatabase(model);
return result;
}我希望它能够为每个不同的模型缓存一个新的结果。目前,它正在缓存第一个结果,即使模型发生了更改,它也返回相同的结果。
我甚至试图为ToString和GetHashCode方法重写ReportFilterModel。实际上,我想要使用更多的属性来生成唯一的HashCode或String。
public override string ToString() {
return SiteId.ToString();
}
public override int GetHashCode() {
return SiteId;
}任何建议,如何让复杂的对象使用OutputCache
发布于 2015-05-22 11:42:40
来自MSDN的VaryByParam值:分号分隔的字符串列表,对应于GET方法的查询字符串值或POST方法的参数值。
如果要根据所有参数值更改输出缓存,请将属性设置为星号(*)。
另一种方法是创建OutputCacheAttribute的子类和用户反射,以创建VaryByParam字符串。就像这样:
public class OutputCacheComplex : OutputCacheAttribute
{
public OutputCacheComplex(Type type)
{
PropertyInfo[] properties = type.GetProperties();
VaryByParam = string.Join(";", properties.Select(p => p.Name).ToList());
Duration = 3600;
}
}在财务主任一职中:
[OutputCacheComplex(typeof (ReportFilterModel))]https://stackoverflow.com/questions/30395376
复制相似问题