HttpRuntime緩存幫助類(lèi)CacheHelper

CacheHelper實(shí)現(xiàn)功能

1,插入緩存

2,獲取緩存對(duì)象

3,刪除緩存數(shù)據(jù)

源碼CacheHelper.cs


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

namespace Helper
{
    /// <summary>
    /// Caching緩存幫助類(lèi)
    /// </summary>
    public class CacheHelper
    {
        //用法
        //插入緩存
        // CacheHelper.Insert(key, list, 60);
        //獲取緩存數(shù)據(jù)
        // list =CacheHelper.GetCache<List<Info>>(key);
        //刪除緩存
        // CacheHelper.Delete(key);
        /// <summary>
        /// 添加到緩存中
        /// </summary>
        /// <param name="key">用于引用對(duì)象的緩存鍵。</param>
        /// <param name="value">要插入到緩存中的對(duì)象</param>
        /// <param name="minutes">所插入對(duì)象將到期并被從緩存中移除的時(shí)間</param>
        public static void Insert(string key, object value, double minutes)
        {
            HttpRuntime.Cache.Insert(key, value, null, DateTime.Now.AddMinutes(minutes), TimeSpan.Zero);
        }
        /// <summary>
        /// 刪除緩存
        /// </summary>
        /// <param name="key"></param>
        public static void Delete(string key)
        {
            HttpRuntime.Cache.Remove(key);
        }
        /// <summary>
        /// 得到緩存中緩存的對(duì)象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">緩存鍵</param>
        /// <returns></returns>
        public static T GetCache<T>(string key)
        {
            object obj = HttpRuntime.Cache.Get(key);
            if (obj != null)
            {
                return (T)obj;
            }
            return default(T);
        }
    }
}

原文鏈接:HttpRuntime緩存幫助類(lèi)CacheHelper