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
  • 相关阅读:
    Codeforces 1190C Tokitsukaze and Duel game
    2019牛客多校第一场E ABBA 贪心 + DP
    Codeforces 1195E OpenStreetMap 单调队列套单调队列
    由 Vue 中三个常见问题引发的深度思考
    jszip打包上传下载
    Ubuntu切换登录用户和root用户
    vue2.0右键菜单
    main.js中import引入css与引入js的区别
    node和npm版本引起的安装依赖和运行项目失败问题
    reduce()之js与python
  • 原文地址:https://www.cnblogs.com/s0611163/p/6243856.html
Copyright © 2011-2022 走看看