Asp.Net Cache辅助类
辅助类代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Caching; namespace BusinessRules.Common { /// <summary> /// Cache高度缓存 /// </summary> public class CacheManager { #region --Property-- private static string _cacheKey = String.Empty; private static String _sqlDepName = "CacheData"; /// <summary> /// 缓存索引键值 /// </summary> public static string CacheKey { get { return CacheManager._cacheKey; } set { CacheManager._cacheKey = value; } } /// <summary> /// 缓存依赖名称 /// </summary> public static String SqlDepName { get { return CacheManager._sqlDepName; } set { CacheManager._sqlDepName = value; } } #endregion #region --Method-- /// <summary> /// 获取当前应用程序指定cacheKey的Cache对象值 /// </summary> /// <param name="cacheKety">索引键值</param> /// <returns>返回对象</returns> public static Object GetCache(String cacheKey) { if (!String.IsNullOrEmpty(cacheKey)) { Cache objCache = HttpRuntime.Cache; return objCache[cacheKey]; } else { return null; } } /// <summary> /// 获取当前应用程序指定cacheKey的Cache对象值 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="cacheKey">索引键值</param> /// <returns>返回对象类型</returns> public static T GetCache<T>(String cacheKey) { object obj = GetCache(cacheKey); return obj == null ? default(T) : (T)obj; } /// <summary> /// 设置当前应用程序指定CacheKey的Cache对象值 /// </summary> /// <param name="cacheKey">索引键值</param> /// <param name="obj">c</param> public static void SetCache(String cacheKey, Object obj) { Cache objCache = HttpRuntime.Cache; objCache.Insert(cacheKey, obj); } /// <summary> /// 创建缓存依赖项 /// </summary> /// <param name="cacheKey"></param> /// <param name="obj"></param> /// <param name="fileName"></param> public static void SetCache(String cacheKey, Object obj, String fileName) { CacheDependency cdep = new CacheDependency(fileName); Cache objCache = HttpRuntime.Cache; objCache.Insert(cacheKey, obj, cdep); } /// <summary> /// 设置当前应用程序指定CacheKey的Cache对象值 /// </summary> /// <param name="cacheKey">索引键值</param> /// <param name="obj">索引键值</param> /// <param name="absoluteExpiration">绝对过期时间</param> /// <param name="slidingExpiration">最后一次访问所插入对象时与该对象过期时之间的时间间隔</param> public static void SetCache(String cacheKey, Object obj, DateTime absoluteExpiration, TimeSpan slidingExpiration) { Cache objCache = HttpRuntime.Cache; objCache.Insert(cacheKey, obj, null, absoluteExpiration, slidingExpiration); } /// <summary> /// 创建缓存项过期 /// </summary> /// <param name="cacheKey">索引键值</param> /// <param name="obj">索引对象</param> /// <param name="expires">过期时间</param> public static void SetCache(String cacheKey, Object obj, Int32 expires) { Cache objCache = HttpRuntime.Cache; objCache.Insert( cacheKey, obj, null, Cache.NoAbsoluteExpiration,//从不过期 new TimeSpan(0, expires, 0) ); } /// <summary> /// 设置依赖的方式缓存数据 /// </summary> /// <param name="cacheKey"></param> /// <param name="obj"></param> /// <param name="cdep"></param> public static void SetCache(String cacheKey, Object obj, CacheDependency cdep) { Cache objCache = HttpRuntime.Cache; objCache.Insert( cacheKey, obj, cdep, Cache.NoAbsoluteExpiration,//从不过期 Cache.NoSlidingExpiration,//禁用可调过期 CacheItemPriority.Default, null ); } //==========调用方式Start=============== // 文件依赖使用说明 // System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency("C:\\test.txt"); // SetCache(CacheKey, objModel, dep);//写入缓存 // // 数据库依赖项使用说明 // System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency("codematic", "P_Product"); // SetCache(CacheKey, objModel, dep);//写入缓存 // //==========调用方式end=============== #endregion } }
调用方式
//设置缓存的键值 CacheManager.CacheKey = "NavMenue"; //获取当前键值下的缓存数据 Object cacheModel = CacheManager.GetCache(CacheManager.CacheKey); //如果缓存中不存在当前键值对象 if (cacheModel == null) { //将数据存储到缓存中 cacheModel = bsnManage.Obj_List(navPosition); if (cacheModel != null) { //依赖数据库的sc_navigation 表变化来更新 SqlCacheDependency scdep = new SqlCacheDependency(CacheManager.SqlDepName, "sc_navigation"); //写入缓存 CacheManager.SetCache(CacheManager.CacheKey, cacheModel, scdep); } return (DataTable)cacheModel; } else return (DataTable)CacheManager.GetCache(CacheManager.CacheKey);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。