asp.net 使用数据缓存

对于一些经常使用而又不经常变更的数据,可以是有数据缓存,以提高效率

缓存类

    /// <summary>
    ///Web缓存操作类
    /// </summary>
    public static class CacheHelper
    {
        // 添加一个缓存 
        public static void SetToCache<T>(string key, T data, int time = 120)
        {
            if (data == null || HttpContext.Current == null)
            {
                return;
            }
            HttpContext.Current.Cache.Insert(
                key,
                data,
                null,
                System.Web.Caching.Cache.NoAbsoluteExpiration,
                TimeSpan.FromMinutes(time)); // Cache的缓存时间,通常建议配置在Config文件中 
        }
        // 按键值获取 T类型的缓存  如果返回值为false,输出T的默认值
        public static bool GetFromCache<T>(string key, out T data)
        {
            var cache = HttpContext.Current == null ? null : HttpContext.Current.Cache[key];
            if (cache == null)
            {
                data = default(T);
                return false;
            }
            data = (T)cache;
            return true;
        }
        /// <summary>
        /// 读取数据
        /// </summary>
        /// <param name="key">键值</param>
        /// <param name="getData">获取数据方法</param>
        /// <param name="time">缓存时间,默认120分钟</param>
        /// <returns>泛型对象</returns>
        public static T Read<T>(string key, Func<T> getData, int time = 120)
        {
            T data;
            if (GetFromCache(key, out data))
            {
                return data;
            }
            data = getData();
            SetToCache(key, data, time);
            return data;
        }
        /// <summary> 
        /// 删除指定的缓存
        /// </summary> 
        /// <param name="key">缓存的键值</param> 
        public static void Remove(string key)
        {
            if (HttpContext.Current != null)
            {
                HttpContext.Current.Cache.Remove(key);
            }
        }
        /// <summary>
        /// 删除所有缓存
        /// </summary>
        public static void RemoveAllCache(string keybegin = "")
        {
            System.Web.Caching.Cache _cache = HttpRuntime.Cache;
            if (_cache != null)
            {
                IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
                ArrayList al = new ArrayList();
                while (CacheEnum.MoveNext())
                {
                    al.Add(CacheEnum.Key);
                }
                foreach (string key in al)
                {
                    if (string.IsNullOrWhiteSpace(keybegin) || (!string.IsNullOrWhiteSpace(keybegin) && key.IndexOf(keybegin) > -1))
                        _cache.Remove(key);
                }
            }
        }
    }
View Code

调用方法

    public class CacheController : Controller
    {
        //
        // GET: /Cache/

        public ActionResult Index()
        {

            return View();
        }

        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <returns></returns>
        public ActionResult SetCache()
        {

            List<Person> list = new List<Person>();

            for (int i = 0; i < 10000; i++)
            {
                Person person = new Person();
                person.Name = "zhu" + i;
                person.Age = i.ToString();
                list.Add(person);
            }

            CacheHelper.SetToCache("perlist", list,2);

            return Content("ok");
        }


        /// <summary>
        /// 获得缓存
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCache()
        {
            List<Person> list = new List<Person>();
            if (CacheHelper.GetFromCache("perlist", out list))
            {
                return Content(list.Where(s => s.Name == "zhu20").FirstOrDefault().Age);
            }
            else
            {
                return Content("no cache");
            }
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public string Age { get; set; }
        
    }
View Code

 

 

asp.net 使用数据缓存,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。