zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    隐藏窗口任务栏图标
    初始化 键盘设备
    web2.0最全的国外API应用集合
    about hadoop-eclipse-plugin used by IDE
    Instructions Set JAVA_HOME System-Wide
    腾讯面试题 腾讯面试题:给40亿个不重复的unsigned int的整数,没排过序的,然后再给一个数,如何快速判断这个数是否在那40亿个数当中?
    word 2013 没有控件菜单怎么办,添加控件菜单
    面试题:实现LRUCache::Least Recently Used的缩写,意思是最近最少使用,它是一种Cache替换算法
    面试题:m个长度为n的ordered array,求top k 个 数字
    面试题: generate an equation, by inserting operator add ("+") and minus ("-") among the array to make equationExpression == 0
  • 原文地址:https://www.cnblogs.com/mibing/p/11424614.html
Copyright © 2011-2022 走看看