缓存技术,封装好的缓存类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace Admin.Helper
- {
-
-
-
- public static class CacheHelper
- {
-
-
-
- private static System.Web.Caching.Cache ObjCache = System.Web.HttpRuntime.Cache;
-
- #region Exist方法
-
-
-
-
-
- public static bool Exist(string Key)
- {
- if (ObjCache[Key] == null)
- {
- return false;
- }
- return true;
- }
- #endregion
-
- #region Get方法
-
-
-
-
-
- public static Object Get(string Key)
- {
- object objkey = null;
- if (ObjCache[Key] != null)
- {
- objkey = ObjCache.Get(Key);
- }
- return objkey;
- }
- #endregion
-
- #region Set方法
-
-
-
-
-
-
-
- public static void Set(string Key, DateTime expiry, object obj)
- {
- if (ObjCache[Key] != null)
- {
- ObjCache.Remove(Key);
- }
-
- ObjCache.Insert(Key, obj, null, expiry, TimeSpan.Zero);
- }
-
-
-
-
-
-
-
- public static void Set(string Key, int min, object obj)
- {
- double douNum = double.Parse(min.ToString());
- Set(Key, DateTime.Now.AddMinutes(douNum), obj);
- }
- #endregion
-
- #region Del方法
-
-
-
-
-
- public static void Del(string Key)
- {
- if (ObjCache[Key] != null)
- {
- ObjCache.Remove(Key);
- }
- }
- #endregion
-
- #region 其他
-
-
-
-
- public static int Count
- {
- get
- {
- return ObjCache.Count;
- }
- }
-
-
-
-
- public static long PrivateBytes
- {
- get
- {
- return ObjCache.EffectivePrivateBytesLimit;
- }
- }
- #endregion
- }
- }
- 用方法如下:
-
-
-
-
-
- public static ProductDto GetProduct(string proId)
- {
- var key = "_ProductId" + proId;
-
- var cache = Helper.CacheHelper.Get(key);
- if (cache != null)
- {
- return (ProductDto)cache;
- }
- var pro = ServiceLocator.Create<IProductService>().Get(proId);
- if (pro == null) return new ProductDto();
- Helper.CacheHelper.Set(key, DateTime.Now.AddHours(8), pro);
- return pro;
- }
原文地址:https://www.cnblogs.com/liuguanghai/p/4702104.html