我用这种方法得到了这个类,我需要支持引用类型和值类型,但我不知道如何执行此操作。任何帮助将不胜感激。
public static class CacheHelper<T>
{
public static T Get(string key, Func<T> function) {
var obj = (T)HttpContext.Current.Cache[key];
if (obj == null)
{
obj = function.Invoke();
HttpContext.Current.Cache.Add(key, obj, null, DateTime.Now.AddMinutes(3),
TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
}
return (T)obj;
}
}
答案 0 :(得分:2)
我认为如果您将此广告单T
移至var obj = HttpContext.Current.Cache[key];
,您应该没问题。
public static class CacheHelper<T>
{
public static T Get(string key, Func<T> function) {
var obj = HttpContext.Current.Cache[key];
if (obj == null)
{
obj = function.Invoke();
HttpContext.Current.Cache.Add(key, obj, null, DateTime.Now.AddMinutes(3),
TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
}
return (T)obj;
}
}