缓存工具类CacheHelper

2018-06-22 06:07:38来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

代码:

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

namespace Common.Utils
{
    /// <summary>
    /// 缓存
    /// </summary>
    public static class CacheHelper
    {
        /// <summary>
        /// 获取缓存
        /// </summary>
        public static object Get(string cacheKey)
        {
            return HttpRuntime.Cache[cacheKey];
        }

        /// <summary>
        /// 设置缓存
        /// </summary>
        public static void Set(string cacheKey, object value)
        {
            HttpRuntime.Cache.Insert(cacheKey, value);
        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        public static void Set<T>(string cacheKey, object value)
        {
            Type type = typeof(T);
            string tableName = type.Name;
            if (HttpRuntime.Cache[tableName] == null)
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add(cacheKey, value);
                HttpRuntime.Cache.Insert(tableName, dic);
            }
            else
            {
                Dictionary<string, object> dic = (Dictionary<string, object>)HttpRuntime.Cache[tableName];
                if (dic.Keys.Contains<string>(cacheKey))
                {
                    dic[cacheKey] = value;
                }
                else
                {
                    dic.Add(cacheKey, value);
                }
                HttpRuntime.Cache[tableName] = dic;
            }
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        public static object Get<T>(string cacheKey)
        {
            Type type = typeof(T);
            string tableName = type.Name;
            if (HttpRuntime.Cache[tableName] != null)
            {
                Dictionary<string, object> dic = (Dictionary<string, object>)HttpRuntime.Cache[tableName];
                if (dic.Keys.Contains<string>(cacheKey))
                {
                    return dic[cacheKey];
                }
            }
            return null;
        }

        /// <summary>
        /// 删除缓存
        /// </summary>
        public static void Remove<T>()
        {
            HttpRuntime.Cache.Remove(typeof(T).Name);
        }
    }
}
View Code

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:在你的ASP.NET MVC中使用查找功能

下一篇:WebApi返回Json格式字符串