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
    }
}


知识共享许可协议
《Cache辅助类》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     158.13ms