1 public class UserBLL 2 { 3 public class UserInfo 4 { 5 public int ID { get; set; } 6 public string Email { get; set; } 7 public string UserName { get; set; } 8 } 9 10 public UserInfo GetModel(int id) 11 { 12 return null;//dal.GetModel(id); 13 } 14 public UserInfo GetModelByCache(int id) 15 { 16 CacheHelper<UserInfo> cacheHelper = new CacheHelper<UserInfo>(); 17 cacheHelper.CacheTime = 10000; 18 cacheHelper.CacheKey = "Model.UserInfo." + id; 19 return cacheHelper.GetValue(id, new CacheHelper<UserInfo>.IntHandel(GetModel)); 20 } 21 22 public UserInfo GetModel(string email) 23 { 24 return null;//dal.GetModel(email); 25 } 26 public UserInfo GetModelByCache(string email) 27 { 28 CacheHelper<UserInfo> cacheHelper = new CacheHelper<UserInfo>(); 29 cacheHelper.CacheTime = 10000; 30 cacheHelper.CacheKey = "Model.UserInfo." + email; 31 return cacheHelper.GetValue(email, new CacheHelper<UserInfo>.StringHandel(GetModel)); 32 } 33 34 public int Update(UserInfo model) 35 { 36 int rtun = 0;//dal.Update(model); 37 if (rtun > 0) 38 { 39 new CacheHelper<UserInfo>() { CacheKey= "Model.UserInfo." + model.Email }.Remove(); 40 new CacheHelper<UserInfo>() { CacheKey = "Model.UserInfo." + model.ID }.Remove(); 41 } 42 return rtun; 43 } 44 } 45 46 public class CacheHelper<T> 47 { 48 /// <summary> 49 /// 多长时间没有访问后过期 50 /// </summary> 51 /// <param name="key"></param> 52 /// <param name="value"></param> 53 /// <param name="time"></param> 54 public static void Insert(string key, object value, TimeSpan time) 55 { 56 if (value != null) 57 { 58 System.Web.HttpRuntime.Cache.Insert( 59 key, 60 value, 61 null, 62 System.Web.Caching.Cache.NoAbsoluteExpiration, 63 time); 64 } 65 } 66 /// <summary> 67 /// 指定时间过期 68 /// </summary> 69 /// <param name="key"></param> 70 /// <param name="value"></param> 71 /// <param name="time">DateTime.UtcNow.AddSeconds(10)</param> 72 public static void Add(string key, object value, DateTime time) 73 { 74 if (value != null) 75 { 76 System.Web.HttpRuntime.Cache.Add( 77 key, 78 value, 79 null, 80 time,//10秒 81 System.Web.Caching.Cache.NoSlidingExpiration, 82 System.Web.Caching.CacheItemPriority.High, 83 null); 84 } 85 } 86 87 public static object Remove(string key) 88 { 89 if (key == null) { return null; } 90 return System.Web.HttpRuntime.Cache.Remove(key); 91 } 92 93 public class CacheObject 94 { 95 //public bool IsExist { get; set; } 96 public T Value { get; set; } 97 } 98 99 public string CacheKey { get; set; } 100 public int CacheTime { get; set; } 101 public IntHandel CacheIntHandel { get; set; } 102 public StringHandel CacheStringHandel { get; set; } 103 104 public delegate T IntHandel(int id); 105 public delegate T StringHandel(string key); 106 107 108 public T GetValue(int id, IntHandel handel) 109 { 110 CacheObject rtun = System.Web.HttpRuntime.Cache[CacheKey] as CacheObject; 111 if (rtun == null) 112 { 113 rtun = new CacheObject(); 114 rtun.Value = handel(id); 115 //rtun.IsExist = rtun.Value != null; 116 Insert(CacheKey, rtun, TimeSpan.FromSeconds(CacheTime)); 117 } 118 return rtun.Value; 119 } 120 public T GetValue(string key, StringHandel handel) 121 { 122 CacheObject rtun = System.Web.HttpRuntime.Cache[CacheKey] as CacheObject; 123 if (rtun == null) 124 { 125 rtun = new CacheObject(); 126 rtun.Value = handel(key); 127 //rtun.IsExist = rtun.Value != null; 128 Insert(CacheKey, rtun, TimeSpan.FromSeconds(CacheTime)); 129 } 130 return rtun.Value; 131 } 132 public T Remove() 133 { 134 CacheObject rtun = System.Web.HttpRuntime.Cache[CacheKey] as CacheObject; 135 if (rtun == null) { return default(T); } 136 return rtun.Value; 137 } 138 }