C#数据 缓存,增加缓存和清除缓存

C#数据缓存
1 引入缓存帮助类

public class CacheHelp
    {

        /// 
        /// 缓存指定对象,设置缓存
        /// 
        /// 缓存的key
        /// 缓存的值
        /// 设置缓存时间;单位:秒
        /// 
        public static bool Set(string key, object value,double Seconds)
        {
            return Set(key, value, null, DateTime.Now.AddSeconds(Seconds), Cache.NoSlidingExpiration,
            CacheItemPriority.Default, null);
        }

        /// 
        /// 缓存指定对象,设置缓存
        /// 
        public static bool Set(string key, object value, string path)
        {
            try
            {
                var cacheDependency = new CacheDependency(path);
                return Set(key, value, cacheDependency);
            }
            catch
            {
                return false;
            }
        }

        /// 
        /// 缓存指定对象,设置缓存
        /// 
        public static bool Set(string key, object value, CacheDependency cacheDependency)
        {
            return Set(key, value, cacheDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
            CacheItemPriority.Default, null);
        }

        /// 
        /// 缓存指定对象,设置缓存
        /// 
        public static bool Set(string key, object value, double seconds, bool isAbsulute)
        {
            return Set(key, value, null, (isAbsulute ? DateTime.Now.AddSeconds(seconds) : Cache.NoAbsoluteExpiration),
            (isAbsulute ? Cache.NoSlidingExpiration : TimeSpan.FromSeconds(seconds)), CacheItemPriority.Default,
            null);
        }

        /// 
        /// 获取缓存对象
        /// 
        public static object Get(string key)
        {
            return GetPrivate(key);
        }

        /// 
        /// 判断缓存中是否含有缓存该键
        /// 
        public static bool Exists(string key)
        {
            return (GetPrivate(key) != null);
        }

        /// 
        /// 移除缓存对象
        /// 
        /// 
        /// 
        public static bool Remove(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return false;
            }
            HttpRuntime.Cache.Remove(key);
            return true;
        }

        /// 
        /// 移除所有缓存
        /// 
        /// 
        public static bool RemoveAll()
        {
            IDictionaryEnumerator iDictionaryEnumerator = HttpRuntime.Cache.GetEnumerator();
            while (iDictionaryEnumerator.MoveNext())
            {
                HttpRuntime.Cache.Remove(Convert.ToString(iDictionaryEnumerator.Key));
            }
            return true;
        }


        /// 
        /// 尝试获取缓存,不存在则执行匿名委托
        /// 
        /// 
        /// 
        /// 
        /// 设置缓存时间;单位:秒
        /// 
        public static T TryGet<T>(string key, Func<T> func, double Seconds)
        {
            if (Exists(key)) return (T)Get(key);
            var result = func.Invoke();
            Set(key, result, Seconds);
            return result;
        }


        /// 
        /// 设置缓存
        /// 
        public static bool Set(string key, object value, CacheDependency cacheDependency, DateTime dateTime,
        TimeSpan timeSpan, CacheItemPriority cacheItemPriority, CacheItemRemovedCallback cacheItemRemovedCallback)
        {
            if (string.IsNullOrEmpty(key) || value == null)
            {
                return false;
            }
            HttpRuntime.Cache.Insert(key, value, cacheDependency, dateTime, timeSpan, cacheItemPriority,
            cacheItemRemovedCallback);
            return true;
        }

        /// 
        /// 获取缓存
        /// 
        private static object GetPrivate(string key)
        {
            return string.IsNullOrEmpty(key) ? null : HttpRuntime.Cache.Get(key);
        }
    }

2 缓存方法

 /// 
        /// 数据缓存处理方法
        /// 
        /// 缓存名称
         /// out 参数判断缓存是否存在
         /// 要缓存的数据 list类型(也可以设置其他类型)
        /// 
        public static List<T> Cache<T>(string key, out bool b,List<T> list = null)
        {
            List<T> listnew =null;
            string CacheKeyUserList = key;
            b = false;
            if (!CacheHelp.Exists(CacheKeyUserList))
            {
                #region 增加缓存

                //造数据(可以直接数据库访问获得数据)
                bool SetCacheStatus = CacheHelp.Set(
                     key: CacheKeyUserList,
                     value: list,
                     Seconds: 300 //缓存时长,单位“秒”
                     );
                if (SetCacheStatus)
                {
                  string str=  "增加缓存成功";
                }
                else
                {
                    string str = "增加缓存失败";
                }
                #endregion
            }
            else
            {
                #region 获取缓存
                listnew =(List<T>)CacheHelp.Get(CacheKeyUserList);
                #endregion
                b = true;
            }
            return listnew;
        }

3 程序中调用

 List<ResArchiveModel> list2 = Cache<ResArchiveModel>(key, out b);
                    //缓存中存在
                    if (b)
                    {
                        result.code = "10000";
                        result.msg = "推荐资讯数据获取成功";
                        result.data = list2.OrderByDescending(s => s.Random).ToList();
                        return SQLBase.toJson(result);
                    }
                    else{
                        //执行逻辑
                        }		

你可能感兴趣的:(C#基础,缓存,c#,redis)