
分享基于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");发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月08日 22时53分54秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
数字印钞界迎来重磅精英机构,普通人还有翻身机会吗? | 加密货币与阶层穿越...
2021-05-10
Dharma暴跌过度解读了吗?去中心化不足,模式难持续是关键
2021-05-10
记录关于C/C++的自学路线
2021-05-10
Ps中的合图过程
2021-05-10
Java初识和开发环境搭建
2021-05-10
Wordpress主题Git后台清净模式设置
2021-05-10
Spring Cloud Dependencies配置maven仓
2021-05-10
JQuery获取元素的方法总结
2021-05-10
张一鸣:创业7年,我经历的5件事
2021-05-10
SQL基础语法
2021-05-10
SQL 已死,但 SQL 将永存
2021-05-10
码农也能有春天:一个人独立运营网站12年,赚到了5亿多美元!
2021-05-10
Python3 日期和时间
2021-05-10
5分钟了解 凭啥Typescript 比JS还火
2021-05-10
JavaScript实现表格排序
2021-05-10
vue散碎知识点学习
2021-05-10
Flask连接Mysql安装及设置操作
2021-05-10
周报二
2021-05-10
周报十一
2021-05-10
成绩登记表(网页设计实验)
2021-05-10