CacheHelper对缓存的控制

通常我们针对页面以及相关数据进行相应的缓存(分为客户端和服务端的缓存),以下代码对一般操作进行相应的缓存(服务端),用以减少对数据库的访问次数,减少服务器的压力.下面写了一个CacheHelper类

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

namespace Asp.netTest.CacheTest
{
    public static class CacheHelper
    {
        public static Cache _cache;

        public static double SaveTime { get; set; }

        static CacheHelper()
        {
            _cache = HostingEnvironment.Cache;//获取缓存实例
            SaveTime = 15.0;
        }

        public static object Get(string key)//获取缓存
        {
            if (string.IsNullOrEmpty(key))
            {
                return null;
            }
            return _cache;
        }

        public static T Get<T>(string key)//获取缓存泛型
        {
            object obj = Get(key);
            return obj == null ? default(T) : (T)obj;
        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <param name="key">用于引用该项的缓存键。</param>
        /// <param name="value">要插入缓存中的对象。</param>
        /// <param name="dependency">该项的文件依赖项或缓存键依赖项。当任何依赖项更改时,该对象即无效,并从缓存中移除。如果没有依赖项,则此参数包含 null。</param>
        /// <param name="priority">该值由缓存在退出对象时使用;具有较低成本的对象在具有较高成本的对象之前被从缓存移除。简单说就是缓存项的优先级</param>
        /// <param name="callback">在从缓存中移除对象时将调用的委托(如果提供)。当从缓存中删除应用程序的对象时,可使用它来通知应用程序。</param>
        public static void Insert(string key, object value, CacheDependency dependency, CacheItemPriority priority, CacheItemRemovedCallback callback)
        {
            _cache.Insert(key, value, dependency, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(SaveTime), priority, callback);
            //Cache.NoAbsoluteExpiration:所插入对象将到期并被从缓存中移除的时间。
            //TimeSpan.FromMinutes(): 最后一次访问所插入对象时与该对象到期时之间的时间间隔。
        }

        public static void Insert(string key, object value, CacheDependency dependency, CacheItemRemovedCallback callback)
        {
            Insert(key, value, dependency, CacheItemPriority.Default, callback);
        }

        public static void Insert(string key, object value, CacheDependency dependency)
        {
            Insert(key, value, dependency, CacheItemPriority.Default, null);
        }

        public static void Insert(string key, object value)
        {
            Insert(key, value, null, CacheItemPriority.Default, null);
        }

        public static void Remove(string key)//移除缓存
        {
            if (string.IsNullOrEmpty(key))
            {
                return;
            }
            _cache.Remove(key);
        }

        public static IList<string> GetKeys()
        {
            List<string> keys = new List<string>();
            IDictionaryEnumerator enumerator = _cache.GetEnumerator();
            while (enumerator.MoveNext())
            {
                keys.Add(enumerator.Key.ToString());
            }
            return keys.AsReadOnly();
        }

        public static void RemoveAll()
        {
            IList<string> keys = GetKeys();
            foreach (string key in keys)
            {
                _cache.Remove(key);
            }
        }
    }
}

在原有基础上增加了如下3个方法:

(1)public static T Get<T>(string key)

(2)public static IList<string> GetKeys()

(3)public static void RemoveAll()

posted @ 2018-11-22 17:07  AngelBoys  阅读(551)  评论(0编辑  收藏  举报