zoukankan      html  css  js  c++  java
  • 缓存工具类CacheHelper

    代码:

    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
  • 相关阅读:
    深浅拷贝的区别
    vue中动态加载组件
    一些工具软件
    echarts 自传作品入口
    Echarts progress 设置背景颜色
    Echarts柱状图,柱子设置不一样的渐变色
    .net 控制台学习笔记 一、控制台程序的异常捕获
    IdentityServer4实战:自定义用户名和密码校验
    IdentityServer4实战:Token 中返回用户属性
    IdentityServer4实战:持久化 Resource
  • 原文地址:https://www.cnblogs.com/s0611163/p/6243856.html
Copyright © 2011-2022 走看看