• 自写缓存类


    public class MyCache
    {
        /// <summary>
        /// 插入一个缓存
        /// </summary>
        /// <param name="name">缓存键</param>
        /// <param name="value">缓存值</param>
        /// <param name="isAbsoulute">是否使用绝对过期,是则为true否则为false</param>
        /// <param name="isOverride">如果缓存存在,是否覆盖缓存值</param>
        /// <param name="absoluteDate">绝对过期时间</param>
        /// <param name="spacing">相对过期的时间间隔</param>
        /// <param name="dep">缓存依赖</param>
        /// <param name="removeddCallback">缓存被移除时调用的委托</param>
        /// <param name="priority">缓存优先级</param>
        public static void Insert(string name, object value, bool isAbsoulute, bool isOverride, DateTime absoluteDate, TimeSpan spacing, CacheDependency dep, CacheItemRemovedCallback removeddCallback, CacheItemPriority priority)
        {

            Cache cache = HttpContext.Current.Cache;
            //如果此键的缓存已存在
            if (cache[name] != null)
            {
                //如果覆盖缓存值
                if (isOverride)
                {
                    cache.Remove(name);
                    InsertInternal(name, value, isAbsoulute, absoluteDate, spacing, dep, removeddCallback, priority);
                }
                else
                {
                    return;
                }
            }
            InsertInternal(name, value, isAbsoulute, absoluteDate, spacing, dep, removeddCallback, priority);
        }

        /// <summary>
        /// 根据键名,获取缓存,如果缓存不存在,则调用返回缓存值的委托
        /// </summary>
        public static object GetCache(string name, Func<object> func, bool isAbsoulute, bool isOverride, DateTime absoluteDate, TimeSpan spacing, CacheDependency dep, CacheItemRemovedCallback removeddCallback, CacheItemPriority priority)
        {
            Cache cache = HttpContext.Current.Cache;
            if (cache[name] != null)
            {
                return cache[name];
            }
            else
            {
                object obj = func();
                Insert(name, obj, isAbsoulute, isOverride, absoluteDate, spacing, dep, removeddCallback, priority);
                return obj;
            }
        }

        /// <summary>
        /// 内部方法
        /// </summary>        
        static void InsertInternal(string name, object value, bool isAbsoulute, DateTime absoluteDate, TimeSpan spacing, CacheDependency dep, CacheItemRemovedCallback callback, CacheItemPriority priority)
        {
            Cache cache = HttpContext.Current.Cache;
            if (isAbsoulute)
            {
                cache.Insert(name, value, dep, absoluteDate, TimeSpan.Zero, priority, callback);
            }
            else
            {
                cache.Insert(name, value, dep, DateTime.MaxValue, spacing, priority, callback);
            }
        }
    }
  • 相关阅读:
    算法笔记--中国剩余定理
    算法笔记--辛普森积分公式
    算法笔记--数学之不定方程解的个数
    算法笔记--卢卡斯定理
    洛谷 P3808 【模板】AC自动机(简单版)洛谷 P3796 【模板】AC自动机(加强版)
    hihocoder #1419 : 后缀数组四·重复旋律4
    codevs 3044 矩形面积求并 || hdu 1542
    Stamps ans Envelope Sive UVA
    洛谷 P2061 [USACO07OPEN]城市的地平线City Horizon
    bzoj 3277: 串
  • 原文地址:https://www.cnblogs.com/mxw09/p/1836199.html
Copyright © 2020-2023  润新知