zoukankan      html  css  js  c++  java
  • Cache Helper类

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Collections;
    using System.Web.Caching;
    
    namespace DMSite.Common
    {
        /// <summary>
        /// 服务器缓存帮助类
        /// </summary>
        public class CacheHelper
        {
            /// <summary>
            /// 创建缓存项的文件依赖
            /// </summary>
            /// <param name="key">缓存Key</param>
            /// <param name="obj">object对象</param>
            /// <param name="fileName">文件绝对路径</param>
            public static void InsertFile(string key, object obj, string fileName)
            {
                //创建缓存依赖项
                CacheDependency dep = new CacheDependency(fileName);
                //创建缓存
                HttpRuntime.Cache.Insert(key, obj, dep);
            }
    
            /// <summary>
            /// 创建缓存项过期
            /// </summary>
            /// <param name="key">缓存Key</param>
            /// <param name="obj">object对象</param>
            public static void Insert(string key, object obj)
            {
                if (obj != null)
                {
                    if (IsExist(key))
                    {
                        HttpRuntime.Cache[key] = obj;
                    }
                    else
                    {
                        int expires = ConvertHelper.ToInt(ConfigHelper.GetAppSettings("TimeCache"));
                        HttpRuntime.Cache.Insert(key, obj, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0));
                    }
                    
                }
            }
            /// <summary>
            /// 判断缓存对象是否存在
            /// </summary>
            /// <param name="strKey">缓存键值名称</param>
            /// <returns>是否存在true 、false</returns>
            public static bool IsExist(string strKey)
            {
                return HttpRuntime.Cache[strKey] != null;
            }
            /// <summary>
            /// 获取缓存对象
            /// </summary>
            /// <param name="key">缓存Key</param>
            /// <returns>object对象</returns>
            public static object GetCache(string key)
            {
                if (string.IsNullOrEmpty(key))
                    return null;
                if (ConfigHelper.GetAppSettings("IsCache") == "false")
                {
                    return null;
                }
    
                return HttpRuntime.Cache.Get(key);
            }
    
            /// <summary>
            /// 获取缓存对象
            /// </summary>
            /// <typeparam name="T">T对象</typeparam>
            /// <param name="key">缓存Key</param>
            /// <returns></returns>
            public static T Get<T>(string key)
            {
                object obj = GetCache(key);
                return obj == null ? default(T) : (T)obj;
            }
    
            /// <summary>
            /// 移除指定数据缓存
            /// </summary>
            public static void RemoveCache(string CacheKey)
            {
                System.Web.Caching.Cache _cache = HttpRuntime.Cache;
                _cache.Remove(CacheKey);
            }
    
            /// <summary>
            /// 移除全部缓存
            /// </summary>
            public static void RemoveAllCache()
            {
                System.Web.Caching.Cache _cache = HttpRuntime.Cache;
                IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
                while (CacheEnum.MoveNext())
                {
                    _cache.Remove(CacheEnum.Key.ToString());
                }
            }
        }
    }
  • 相关阅读:
    11. Container With Most Water(装最多的水 双指针)
    64. Minimum Path Sum(最小走棋盘 动态规划)
    数组相关
    88. Merge Sorted Array(从后向前复制)
    京东AI平台 春招实习生面试--NLP(offer)
    54. Spiral Matrix(矩阵,旋转打印)
    48. Rotate Image(旋转矩阵)
    春招实习--阿里 蚂蚁金服 支付宝 机器学习面试
    26. Remove Duplicates from Sorted Array(删除排序数组中的重复元素,利用排序的特性,比较大小)
    7. Reverse Integer(翻转整数)
  • 原文地址:https://www.cnblogs.com/damsoft/p/6100308.html
Copyright © 2011-2022 走看看