C# CacheHelper的编程实现

来源:互联网 发布:微信圈粉软件 编辑:程序博客网 时间:2024/05/22 12:42
 
using System;using System.Web;using System.Xml.Linq;using System.Web.Caching;using System.Collections.Generic;using BIReportCenter.ServiceImp.Config;using Common.HelperObjects;namespace BIReportCenter.ServiceImp.Core{    public delegate object GetCacheMapper();    public delegate T GetCacheInstanceMapper<T>();    public delegate List<T> GetCacheInstanceListMapper<T>();    public delegate XDocument GetCacheXmlMapper();    public static class CacheHelper    {        /// <summary>        /// 获取当前appdomain中的cache        /// </summary>        public static Cache CurrentCache        {            get { return HttpRuntime.Cache; }        }        public static BIReportCenterConfig BIRccInstance        { get { return BIReportCenterConfig.Instance; } }       /// <summary>        /// 向当前缓存中插入键值数据        /// </summary>        /// <param name="key"></param>        /// <param name="obj"></param>        /// <param name="expiredDate"></param>        /// <param name="sqlDependency"></param>        public static void Add(string key, object obj, DateTime expiredDate, CacheDependency sqlDependency = null)        {            if (obj != null)                CurrentCache.Insert(key,                                    obj,                                    sqlDependency,                                    expiredDate,                                    TimeSpan.Zero);        }               /// <summary>        /// 从缓存中获取指定键名的数据        /// </summary>        /// <param name="key"></param>        /// <returns></returns>        public static object Get(string key)        {            return CurrentCache.Get(key);        }                /// <summary>        /// 从缓存中清除指定键名的数据        /// </summary>        /// <param name="key"></param>        /// <returns></returns>        public static object Remove(string key)        {            return CurrentCache.Remove(key);        }        /// <summary>        /// 从IIS Cache中获取缓存对象        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="key"></param>        /// <param name="expiredDate"></param>        /// <returns></returns>        private static T GetInstanceByIIS<T>(string key)        {            ArgumentHelper.AssertNotNullOrEmpty(key);            object result = Get(key);            if (result == null)            {                return default(T);            }            return (T)result;        }        /// <summary>        /// 是否启用IISCache        /// </summary>        /// <returns></returns>        public static Boolean IISCacheEnable()        {            return BIRccInstance.IISConfigs[0].Enable;        }        /// <summary>        /// 查询对象        /// </summary>        /// <typeparam name="T">对象类型</typeparam>        /// <param name="key">检索缓存的key</param>        /// <param name="cacheMapper">数据源</param>        /// <returns></returns>        public static T GetInstance<T>(string key, GetCacheInstanceMapper<T> cacheMapper, CacheDependency sqlDependency = null)        {            ArgumentHelper.AssertNotNullOrEmpty(key);            T result = default(T);            //获取缓存类型 key            string cacheTypeKey = GetCacheTypeKey(key);            if (IISCacheEnable())            {                //从IIS Cache中获取对象                result = GetInstanceByIIS<T>(key);            }            if (object.Equals(result, default(T))) //说明从IISCache中没有取到对象。            {                //从数据库获取数据                result = cacheMapper();                //根据缓存类型key 获取该类型过期时间                int cacheTimeOut = GetCacheTimeOut(cacheTypeKey);                //给datarelay Cache对象赋值。                if (IISCacheEnable())                    Add(key, result, DateTime.Now.AddMinutes(cacheTimeOut), sqlDependency);            }            return result;        }        /// <summary>        /// 得到过期时间        /// </summary>        /// <param name="key"></param>        /// <returns></returns>        public static int GetCacheTimeOut(string cacheKey)        {            ArgumentHelper.AssertNotNullOrEmpty(cacheKey);            int result = 30;            bool isContainKey = false;            if (BIRccInstance.CacheTimeOut != null && BIRccInstance.CacheTimeOut.Count > 0)            {                foreach (CacheItem ci in BIRccInstance.CacheTimeOut)                {                    isContainKey = (isContainKey || string.Equals(ci.Name, cacheKey, StringComparison.OrdinalIgnoreCase));                    if (isContainKey)                    {                        result = ci.TimeOut;                        return result;                    }                }            }            return result;        }        /// <summary>        /// 获取缓存类型key        /// </summary>        /// <param name="key"></param>        /// <returns></returns>        public static string GetCacheTypeKey(string key)        {            ArgumentHelper.AssertNotNullOrEmpty(key);            return key.Split(new char[] { '≮' })[0];        }    }}