cache操作类

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Collections;
using System.Web.Caching;


namespace yang.Utility
{
    public static class CacheHelper
    {
        static Cache WebCache;
        static HWCache TempCache=new HWCache ();
        static bool IsWebCacheGet()
        {
            if (WebCache == null)
            {
                if (HttpContext.Current != null && HttpContext.Current.Cache != null)
                {
                    WebCache = HttpContext.Current.Cache;
                    return true;
                }
                else
                    return false;
            }
            return true;
        }
       
        public static void Insert(string CacheName,Object CacheObject,int CacheMinutes)
        {
            if (!string.IsNullOrEmpty(CacheName) && CacheObject!=null)
                if (IsWebCacheGet())
                    WebCache.Insert(CacheName, CacheObject, null, DateTime.Now.AddMinutes(CacheMinutes), System.Web.Caching.Cache.NoSlidingExpiration);
                else
                {
                    TempCache.Insert(CacheName, CacheObject, CacheMinutes);
                }
        }
        public static object Get(string CacheName)
        {
            object CacheObject=null;
            if (!string.IsNullOrEmpty(CacheName) )
                if (IsWebCacheGet())
                    CacheObject = WebCache[CacheName];
                else
                {
                    CacheObject = TempCache.Get (CacheName);
                }
            return CacheObject;
        }
        public static void Remove(string CacheName)
        {          
            if (!string.IsNullOrEmpty(CacheName))
                if (IsWebCacheGet())
                    WebCache.Remove(CacheName);
                else
                {
                    TempCache.Remove(CacheName);
                }
        }
        public static int Clear(string CachePreStr)
        {
            int CacheCount = 0;
            if (IsWebCacheGet())
            {
                CacheCount = WebCache.Count;
                IDictionaryEnumerator CacheEnum = WebCache.GetEnumerator();
                while (CacheEnum.MoveNext())
                {
                    WebCache.Remove(CacheEnum.Key.ToString());
                }
                TempCache.Clear();
            }
            else
            {
                CacheCount = TempCache.Clear();
            }
            string TempLog = "清空了" + CacheCount.ToString() + "条缓冲记录";
            LogHelper.Warn("CacheHelper", TempLog);
            return CacheCount;
        }
    }
    /// <summary>
    /// 自定义缓存
    /// </summary>
    public  class HWCache:IDisposable
    {
        static List<HWCache> CacheList = new List<HWCache>();
        /// <summary>
        /// 缓存Hash
        /// </summary>
        Hashtable CacheObjectHash = new Hashtable();
        /// <summary>
        /// 缓存时间Hash
        /// </summary>
        Hashtable CacheTimeHash = new Hashtable();
        /// <summary>
        /// 自动移除
        /// </summary>
        bool AutoRemove = true;
        /// <summary>
        /// 自定义缓存
        /// </summary>
        public HWCache()
        {
            this.AutoRemove = true;
            CacheList.Add(this);
        }
        /// <summary>
        /// 自定义缓存
        /// </summary>
        /// <param name="AutoRemove">自动移除</param>
        public HWCache(bool AutoRemove)
        {
            this.AutoRemove = AutoRemove;
            CacheList.Add(this);
        }

        public void Insert(string CacheName, object CacheObject, int CacheMinutes)
        {
            CacheObjectHash.Remove(CacheName);
            CacheTimeHash.Remove(CacheName);
            CacheObjectHash.Add(CacheName, CacheObject);
            CacheTimeHash.Add(CacheName, DateTime.Now.AddMinutes(CacheMinutes));
        }
        public object Get(string CacheName)
        {
            return Get(CacheName, true);
        }
        /// <summary>
        /// 获取缓存数据
        /// </summary>
        /// <param name="CacheName">缓存名称</param>
        /// <param name="TimeLimit">是否采用时间限制,默认采用,不采用只有在AutoRemove为Fasle才有效果</param>
        /// <returns></returns>
        public object Get(string CacheName,bool TimeLimit)
        {
            object CacheObject = CacheObjectHash[CacheName];
            if (CacheObject != null&&TimeLimit )
            {
                DateTime CacheLimit = (DateTime)(CacheTimeHash[CacheName]);
                if (CacheLimit < DateTime.Now)
                {
                    CacheObject = null;
                    if (AutoRemove)
                        Remove(CacheName);
                }
            }
            return CacheObject;
        }


        public void Remove(string CacheName)
        {
            CacheObjectHash.Remove(CacheName);
            CacheTimeHash.Remove(CacheName);
        }

        public int Clear()
        {
            int Count=CacheObjectHash.Count;           
            CacheObjectHash.Clear();
            CacheTimeHash.Clear();
            return Count;
        }

        #region IDisposable 成员

        public void Dispose()
        {
            CacheList.Remove(this);
        }

        #endregion
    }
}

转载于:https://www.cnblogs.com/yshj/archive/2012/11/29/2762791.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值