C#.NET WINFORM 缓存 System.Runtime.Caching MemoryCache

工具类:

using System;
using System.Runtime.Caching; namespace CommonUtils
{
/// <summary>
/// 基于MemoryCache的缓存辅助类
/// </summary>
public static class MemoryCacheHelper
{ public static void SetCache<T>(string key, T cacheItem, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null)
{
if (String.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("Invalid cache key");
}
if (cacheItem == null)
{
throw new ArgumentNullException("cacheItem null ");
}
if (slidingExpiration == null && absoluteExpiration == null)
{
throw new ArgumentException("Either a sliding expiration or absolute must be provided");
}
MemoryCache.Default.Remove(key); var item = new CacheItem(key, cacheItem);
var policy = CreatePolicy(slidingExpiration, absoluteExpiration);
MemoryCache.Default.Add(item, policy);
} public static T GetCache<T>(string key)
{
if (String.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("Invalid cache key");
}
return (T)MemoryCache.Default[key];
} /// <summary>
/// 移除缓存
/// </summary>
/// <param name="key"></param>
public static void RemoveCache(string key)
{
MemoryCache.Default.Remove(key);
} 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;
}
}
}

使用:

string cacheKey = "person_key";
MemoryCacheHelper.SetCache<Person>(cacheKey,
new Person() { Id = Guid.NewGuid(), Name = "wolfy" }
, new TimeSpan(0, 30, 0)); Console.WriteLine("获取缓存中数据");
Person p2 = MemoryCacheHelper.GetCache<Person>(cacheKey);
if (p2 != null)
{
Console.WriteLine(p2.ToString());
}
MemoryCacheHelper.RemoveCache(cacheKey);
Person p3 = MemoryCacheHelper.GetCache<Person>(cacheKey);
if (p3 != null)
{
Console.WriteLine(p3.ToString());
}
Console.Read();

--

C#.NET WINFORM 缓存 System.Runtime.Caching MemoryCache的更多相关文章

  1. C# System.Runtime.Caching使用

    System.Runtime.Caching命名空间是.NET 4.0新增的,目的是将以前的.NET 版本中的System.Web.Caching单独提取出来,独立使用,这样web和其他.NET程序如 ...

  2. HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching

    先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前应用程序的Cac ...

  3. System.Runtime.Caching中MemoryCache帮助类

    值得参考的几个内存缓存帮助类: 参考资料: https://github.com/Hendy/memory-cache-helper https://gist.github.com/jdalley/0 ...

  4. 缓存-System.Web.Caching.Cache

    实现 Web 应用程序的缓存. 每个应用程序域创建一个此类的实例,只要应用程序域将保持活动状态,保持有效. 有关此类的实例的信息,请通过Cache的属性HttpContext对象或Cache属性的Pa ...

  5. [Winform]缓存处理

    摘要 在对winform做的项目优化的时候,首先想到的是对查询,并不经常变化的数据进行缓存,但对web项目来说有System.Web.Caching.Cache类进行缓存,那么winform端该如何呢 ...

  6. System.Web.Caching.Cache类 缓存

    1.文件缓存依赖 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender ...

  7. System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...

  8. 深入System.Web.Caching命名空间 教你Hold住缓存管理

    一,System .Web.Caching与缓存工作机制简介 System.Web.Caching是用来管理缓存的命名空间,其父级空间是System.Web,由此可见,缓存通常用于Web网站的开发,包 ...

  9. System.Web.Caching.Cache类 缓存 各种缓存依赖(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...

  10. System.Web.Caching.Cache类 Asp.Net缓存 各种缓存依赖

    Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 一.属性 属性 说明 Count 获取存储在缓存中的 ...

随机推荐

  1. CF1857B Maximum Rounding 题解

    题目描述 给定一个自然数 \(n\),可以对任意一位进行四舍五入,可以进行任意次,求能得到的最大数.(这里的 \(n\) 没有前导零) 思路 首先我们发现,如果我们将其中一位进位了,那后面的所有位都会 ...

  2. 双龙贺岁,龙蜥 LoongArch GA 版正式发布

    ​简介:Anolis OS 8.4 LoongArch 正式版发布产品包括 ISO.软件仓库.虚拟机镜像.容器镜像. ​ 简介 继  Anolis OS LoongArch 预览版发布后,现迎来龙蜥 ...

  3. Log4j漏洞不仅仅是修复,更需要构建有效预警机制

    ​简介:软件的漏洞有时不可避免,根据Gartner的相关统计,到 2025 年,30% 的关键信息基础设施组织将遇到安全漏洞.日志服务SLS,可帮助快速部署一个预警机制,使得漏洞被利用时可以快速发现并 ...

  4. 双11特刊|一站式在线数据管理平台DMS技术再升级,高效护航双11

    ​简介: 10万+企业共同选择的数据库服务平台 阿里云数据库已连续多年稳定支撑天猫双11,历经极端流量场景淬炼.除了保障稳定顺滑的基本盘,今年大促期间数据库通过全面云原生化,大幅提升用户体验,让技术帮 ...

  5. [FAQ] Edge/Chrome 网络请求的编辑并重发

    1. 在网络请求上面右键,复制为fetch. 2. 切换到Console控制台,粘贴并回车. fetch 是javascript中一个网络请求的函数或者工具,Chrome在我们 Copy as fet ...

  6. WPF 不安装 WindowsAppSDK 使用 WinRT 功能的方法

    安装 Microsoft.WindowsAppSDK 库会限制应用程序只能分发 windows 10 应用,如果自己的应用程序依然需要兼容 Win7 等旧系统,那直接采用安装 WindowsAppSD ...

  7. foreach更改element内容后this到data不生效导致页面数据无变化

    list.forEach(element => {  element = element.split('^')    console.log(element)  }) 数据已经被更改,但在外部t ...

  8. RK 平台安装 ubuntu 系统

    一.简介 之前有介绍到 ARM 平台移植 ubuntu 的操作流程,在 RK 系列的平台同样适用,所以这里就不介绍怎么一步步的去对 ubuntu 进行移植,而是怎么将移植的过程编写成脚本,这样便可以在 ...

  9. 01、Windows 排查

    Windows 分析排查 分析排查是指对 Windows 系统中的文件.进程.系统信息.日志记录等进行检测,挖掘 Windows 系统中是否具有异常情况 1.开机启动项检查 一般情况下,各种木马.病毒 ...

  10. 机器学习策略篇:详解开发集和测试集的大小(Size of dev and test sets)

    在深度学习时代,设立开发集和测试集的方针也在变化. 可能听说过一条经验法则,在机器学习中,把取得的全部数据用70/30比例分成训练集和测试集.或者如果必须设立训练集.开发集和测试集,会这么分60%训练 ...