C# CacheHelper的编程实现

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

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值