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. 力扣915(java&python)-分割数组(中等)

    题目: 给定一个数组 nums ,将其划分为两个连续子数组 left 和 right, 使得: left 中的每个元素都小于或等于 right 中的每个元素.left 和 right 都是非空的.le ...

  2. 力扣481(java&python)-神奇字符串(中等)

    题目: 神奇字符串 s 仅由 '1' 和 '2' 组成,并需要遵守下面的规则: 神奇字符串 s 的神奇之处在于,串联字符串中 '1' 和 '2' 的连续出现次数可以生成该字符串.s 的前几个元素是 s ...

  3. 「开源人说」|AI普惠,阿里灵杰开源历程与思考

    简介: 施兴 阿里巴巴资深技术专家 阿里巴巴开源项目EasyRec负责人 以下为开发者社区「开源人说」第四期--大数据&AI专场的主题分享,点击链接了解更多精彩详情 https://devel ...

  4. mPaas 研发流程和线上运维介绍

    简介: mPaas 研发流程和线上运维介绍 一. 背景 金融级移动开发平台 mPaaS[1](Mobile PaaS)为 App 开发.测试.运营及运维提供云到端的一站式解决方案,能有效降低技术门槛. ...

  5. Flow vs Jenkins 实操对比,如何将Java应用快速发布至ECS

    ​简介:Jenkins 由于其开源特性以及丰富插件能力,长久以来都是中小企业搭建 CICD 流程的首选.不过 Jenkins 存在维护成本高.配置复杂等缺点,云效 Flow 较好地解决了这些问题. 本 ...

  6. IIncrementalGenerator 获取项目默认命名空间

    本文将告诉大家如何在分析器里面获取到项目的默认命名空间 在 Roslyn 分析器里面读取项目的默认命名空间,可以通过读取项目的属性配置实现.通过 IIncrementalGenerator 增量 So ...

  7. dotnet 6 创建进程 Process.Start 时设置 UseShellExecute 在 Windows 下对性能的影响

    本文将告诉大家,在 dotnet 6 或 dotnet 7 版本里,启动新的进程时,在 StartInfo 设置 UseShellExecute 为 true 和 false 时,对性能的影响 在 d ...

  8. 第一讲 Cadence-于真博士课程简介

    第一讲 Cadence-于真博士课程简介 以工程化的思想来学习本课程,比较有效,从一个DSP开发板设计为例开始,做出PCB板,顺带学会Cadence软件操作,而不是仅仅学习软件. 前期准备工作,阅读配 ...

  9. ITSM2023年十大功能趋势[采和]

    总体描述:更加人性化,引入自动化相关的设计和技术,更加实用好用.1. 100%服务目录服务目录必须完全贴合用户方的运维实际开展的 服务清单,而不是想当然的抄书或者臆想!都2023年了,还有完全不着调的 ...

  10. go实现发送邮件验证码

    目录 开启SMTP服务: 发邮件测试 业务实现 开启SMTP服务: QQ邮箱参考下面连接: QQ邮箱如何开通SMTP服务 https://jingyan.baidu.com/article/00a07 ...