zoukankan      html  css  js  c++  java
  • cache缓存帮助类

    public class CacheHelper
        {
            /// <summary>
            /// 创建缓存项的文件
            /// </summary>
            /// <param name="key">缓存Key</param>
            /// <param name="obj">object对象</param>
            public static void Insert(string key, object obj)
            {
                //创建缓存
                HttpContext.Current.Cache.Insert(key, obj);
            }
            /// <summary>
            /// 移除缓存项的文件
            /// </summary>
            /// <param name="key">缓存Key</param>
            public static void Remove(string key)
            {
                //创建缓存
                HttpContext.Current.Cache.Remove(key);
            }
            /// <summary>
            /// 创建缓存项的文件依赖
            /// </summary>
            /// <param name="key">缓存Key</param>
            /// <param name="obj">object对象</param>
            /// <param name="fileName">文件绝对路径</param>
            public static void Insert(string key, object obj, string fileName)
            {
                //创建缓存依赖项
                CacheDependency dep = new CacheDependency(fileName);
                //创建缓存
                HttpContext.Current.Cache.Insert(key, obj, dep);
            }
    
            /// <summary>
            /// 创建缓存项过期
            /// </summary>
            /// <param name="key">缓存Key</param>
            /// <param name="obj">object对象</param>
            /// <param name="expires">过期时间(分钟)</param>
            public static void Insert(string key, object obj, int expires)
            {
                HttpContext.Current.Cache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0));
            }
    
            /// <summary>
            /// 获取缓存对象
            /// </summary>
            /// <param name="key">缓存Key</param>
            /// <returns>object对象</returns>
            public static object Get(string key)
            {
                if (string.IsNullOrEmpty(key))
                {
                    return null;
                }
                return HttpContext.Current.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 = Get(key);
                return obj == null ? default(T) : (T)obj;
            }
    
        }
  • 相关阅读:
    hdu 1272
    BZOJ_3685_普通van Emde Boas树_权值线段树
    BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP
    BZOJ_3252_攻略_线段树+dfs序
    BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针
    BZOJ_3210_花神的浇花集会_切比雪夫距离
    BZOJ_2124_等差子序列_线段树+Hash
    BZOJ_2212_[Poi2011]Tree Rotations_线段树合并
    BZOJ_1826_[JSOI2010]缓存交换 _线段树+贪心
    BZOJ_4325_NOIP2015 斗地主_DFS
  • 原文地址:https://www.cnblogs.com/yuanyanyan/p/6044036.html
Copyright © 2011-2022 走看看