分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!
发布日期:2021-05-09 09:35:27 浏览次数:9 分类:博客文章

本文共 6130 字,大约阅读时间需要 20 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Caching;using System.Text;using System.Threading.Tasks;namespace AutoLogisticsPH.Common.Utils{    ///     /// ������MemoryCache������������������������������������    /// Author������������    /// Date���2017/12/11    ///     public static class MemoryCacheUtil    {        private static readonly Object _locker = new object(), _locker2 = new object();        ///         /// ������������������������������������������        ///         /// 
/// ///
public static T GetCacheItem
(String key) { try { return (T)MemoryCache.Default[key]; } catch { return default(T); } } ///
/// ��������������������������������� /// ///
///
public static bool Contains(string key) { return MemoryCache.Default.Contains(key); } ///
/// ������������,��������������������������������� /// ///
///
///
///
///
///
public static T GetOrAddCacheItem
(String key, Func
cachePopulate, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null) { if (String.IsNullOrWhiteSpace(key)) throw new ArgumentException("Invalid cache key"); if (cachePopulate == null) throw new ArgumentNullException("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) //���������������������������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
(String key, Func
cachePopulate, string dependencyFilePath) { if (String.IsNullOrWhiteSpace(key)) throw new ArgumentException("Invalid cache key"); if (cachePopulate == null) throw new ArgumentNullException("cachePopulate"); if (MemoryCache.Default[key] == null) { lock (_locker2) { if (MemoryCache.Default[key] == null) { T cacheValue = cachePopulate(); if (!typeof(T).IsValueType && ((object)cacheValue) == null) //���������������������������NULL��������������� { return cacheValue; } var item = new CacheItem(key, cacheValue); var policy = CreatePolicy(dependencyFilePath); MemoryCache.Default.Add(item, policy); } } } return (T)MemoryCache.Default[key]; } ///
/// ��������������������������� /// ///
public static void RemoveCacheItem(string key) { try { MemoryCache.Default.Remove(key); } catch { } } private static CacheItemPolicy CreatePolicy(TimeSpan? slidingExpiration, DateTime? absoluteExpiration) { var policy = new CacheItemPolicy(); if (absoluteExpiration.HasValue) { policy.AbsoluteExpiration = absoluteExpiration.Value; } else if (slidingExpiration.HasValue) { policy.SlidingExpiration = slidingExpiration.Value; } policy.Priority = CacheItemPriority.Default; return policy; } private static CacheItemPolicy CreatePolicy(string filePath) { CacheItemPolicy policy = new CacheItemPolicy(); policy.ChangeMonitors.Add(new HostFileChangeMonitor(new List
() { filePath })); policy.Priority = CacheItemPriority.Default; return policy; } }}

������������������������������������������������������������������������  ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������CONFIG���������������������������������������������������CONFIG���������������������������������������������������������������������������������������������������������������������CONFIG���

������������������������(������������������������������������������������������������������connstr���������������������������������KEY dbConnName���������������������������������������������������������������������CONFIG���������������������������������������������������������������������������������������������)
            string connstr= MemoryCacheUtil.GetOrAddCacheItem(dbConnName, () =>
            {
                var connStrSettings = ConfigUtil.GetConnectionString(dbConnName,dbConnectionStringConfigPath);
                string dbProdName = connStrSettings.ProviderName;
                string dbConnStr = connStrSettings.ConnectionString;
                return EncryptUtil.Decrypt(dbConnStr);
            }, "���������������������������������c:\app\app.config");

上一篇:封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil
下一篇:适用于app.config与web.config的ConfigUtil读写工具类

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月08日 22时53分54秒