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

  • 相关阅读:
    使用docker试用各种软件及docker-ES设置
    Atom读写MarkDown插件选择,以及墙内安装markdown-preview-enhanced,及markdown和mermaid使用教程
    杂谈迁移tomcat项目到docker,以及遇到的问题
    MongoSpark 28799错误
    MongoDB运维心得(一)
    Ubuntu16.04 appstreamcli错误
    MongoExport后的负载均衡问题查询及解决:can't accept new chunks because there are still 2 deletes from previous migration
    MongoDB集群单mongos的问题总结
    Linux上不了网的几个排查点
    git忽略某些文件的几种方法
  • 原文地址:https://www.cnblogs.com/yshj/p/2762791.html
Copyright © 2020-2023  润新知