HttpRuntime缓存帮助类CacheHelper

CacheHelper实现功能

1,插入缓存

2,获取缓存对象

3,删除缓存数据

源码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缓存帮助类
    /// </summary>
    public class CacheHelper
    {
        //用法
        //插入缓存
        // CacheHelper.Insert(key, list, 60);
        //获取缓存数据
        // list =CacheHelper.GetCache<List<Info>>(key);
        //删除缓存
        // CacheHelper.Delete(key);
        /// <summary>
        /// 添加到缓存中
        /// </summary>
        /// <param name="key">用于引用对象的缓存键。</param>
        /// <param name="value">要插入到缓存中的对象</param>
        /// <param name="minutes">所插入对象将到期并被从缓存中移除的时间</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>
        /// 得到缓存中缓存的对象
        /// </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);
        }
    }
}