• memoryCache的使用


    1 借鉴这篇文章

    https://www.cnblogs.com/zuowj/p/8440902.html

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.Caching;
    using System.Diagnostics;
    
    namespace ConsoleApp1
    {
        public static class MemoryCacheHelper
        {
            private static readonly object _locker = new object();
            private static readonly object _locker2 = new object();
    
    
            /// <summary>
            /// 根据key取缓存,不存在则返回null
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="key"></param>
            /// <returns></returns>
            public static T GetCacheItem<T>(String key)
            {
                try
                {
                    return (T)MemoryCache.Default[key];
                }
                catch (Exception ex)
                {
    
                    return default(T);
                }
            }
    
            /// <summary>
            /// 是否包含指定键的缓存项
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public static bool Contains(string key)
            {
                return MemoryCache.Default.Contains(key);
            }
    
            public static T GetOrAddCacheItem<T>(string key, Func<T> cachePopulate, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null)
            {
                if (string.IsNullOrWhiteSpace(key))
                {
                    throw new ArgumentException("Invalid cache key");
                }
                if (cachePopulate == null)
                {
                    throw new ArgumentException("cachePopulate");
                }
                if (slidingExpiration == null && absoluteExpiration == null)
                {
                    throw new ArgumentException("Either a sliding expiration or absolute must be provided");
                }
                if (MemoryCache.Default[key] == null)
                {
                    lock (_locker)
                    {
                        if (MemoryCache.Default[key] == null)
                        {
                            T cacheValue = cachePopulate();
                            if (!typeof(T).IsValueType && ((object)cacheValue) == null)
                            {
                                return cacheValue;
                            }
                            var item = new CacheItem(key, cacheValue);
                            var policy = CreatePolicy(slidingExpiration, absoluteExpiration);
                            MemoryCache.Default.Add(item, policy);
                        }
                       
                    }
                }
    
                return (T)MemoryCache.Default[key];
            }
    
            public static T GetOrAddCacheItem<T>(string key,Func<T> cachePopulate,string dependencyFilePath)
            {
                if (string.IsNullOrWhiteSpace(key))
                {
                    throw new ArgumentException("Invalid cache key");
                }
                if (cachePopulate == null)
                {
                    throw new ArgumentException("cachePopulate");
                }
    
                if (MemoryCache.Default[key]==null)
                {
                    lock (_locker2)
                    {
                        if (MemoryCache.Default[key] == null)
                        {
                            T cacheValue = cachePopulate();
                            if (!typeof(T).IsValueType && ((object)cacheValue) == null)
                            {
                                return cacheValue;
                            }
                            var item = new CacheItem(key, cacheValue);
                            var policy = CreatePolicy(dependencyFilePath);
                            MemoryCache.Default.Add(item, policy);
                        }
                    }
                }
    
                return (T)MemoryCache.Default[key];
            }
    
            /// <summary>
            /// 移除指定键的缓存项
            /// </summary>
            /// <param name="key"></param>
            public static void RemoveCacheItem(string key)
            {
                try
                {
                    MemoryCache.Default.Remove(key);
                }
                catch (Exception)
                {
    
                }
            }
    
            private static CacheItemPolicy CreatePolicy(TimeSpan? slidingExpiration, DateTime? absoluteExpiration)
            {
                var policy = new CacheItemPolicy();
                if (slidingExpiration.HasValue)
                {
                    policy.SlidingExpiration = slidingExpiration.Value;
                }
                else if (absoluteExpiration.HasValue)
                {
                    policy.AbsoluteExpiration = absoluteExpiration.Value;
                }
    
                policy.Priority = CacheItemPriority.Default;
                return policy;
            }
    
            /// <summary>
            /// 缓存文件
            /// </summary>
            /// <param name="filepath"></param>
            /// <returns></returns>
            private static CacheItemPolicy CreatePolicy(string filepath)
            {
                var policy = new CacheItemPolicy();
                policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List<string>() { filepath }));
    
                policy.Priority = CacheItemPriority.Default;
                return policy;
            }
        }
    }
  • 相关阅读:
    Java实现 蓝桥杯VIP 算法提高 Torry的困惑(提高型)
    Java实现 蓝桥杯VIP 算法提高 Torry的困惑(提高型)
    Java实现 蓝桥杯VIP 算法提高 Torry的困惑(提高型)
    Java实现 蓝桥杯VIP 算法提高 计算时间
    关于编译器和链接器的一个实验
    Windows下获取逻辑cpu数量和cpu核数量(用GetLogicalProcessorInformation,从XP3才开始有的API)
    计算机底层数据的处理方式(汇编后将所有数据都转化为补码二进制数据,所有类型信息都会消失)
    值得推荐的C/C++框架和库
    Delphi子类调用祖父类的虚函数
    [Framework Design Guideline]
  • 原文地址:https://www.cnblogs.com/mibing/p/11424614.html
Copyright © 2020-2023  润新知