C#.NET WINFORM 缓存 System.Runtime.Caching MemoryCache
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的更多相关文章
- C# System.Runtime.Caching使用
System.Runtime.Caching命名空间是.NET 4.0新增的,目的是将以前的.NET 版本中的System.Web.Caching单独提取出来,独立使用,这样web和其他.NET程序如 ...
- HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching
先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cac ...
- System.Runtime.Caching中MemoryCache帮助类
值得参考的几个内存缓存帮助类: 参考资料: https://github.com/Hendy/memory-cache-helper https://gist.github.com/jdalley/0 ...
- 缓存-System.Web.Caching.Cache
实现 Web 应用程序的缓存. 每个应用程序域创建一个此类的实例,只要应用程序域将保持活动状态,保持有效. 有关此类的实例的信息,请通过Cache的属性HttpContext对象或Cache属性的Pa ...
- [Winform]缓存处理
摘要 在对winform做的项目优化的时候,首先想到的是对查询,并不经常变化的数据进行缓存,但对web项目来说有System.Web.Caching.Cache类进行缓存,那么winform端该如何呢 ...
- System.Web.Caching.Cache类 缓存
1.文件缓存依赖 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender ...
- System.Web.Caching.Cache类 缓存 各种缓存依赖
原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...
- 深入System.Web.Caching命名空间 教你Hold住缓存管理
一,System .Web.Caching与缓存工作机制简介 System.Web.Caching是用来管理缓存的命名空间,其父级空间是System.Web,由此可见,缓存通常用于Web网站的开发,包 ...
- System.Web.Caching.Cache类 缓存 各种缓存依赖(转)
转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...
- System.Web.Caching.Cache类 Asp.Net缓存 各种缓存依赖
Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 一.属性 属性 说明 Count 获取存储在缓存中的 ...
随机推荐
- Flink 源码 | 自定义 Format 消费 Maxwell CDC 数据
Flink 1.11 最重要的 Feature -- Hive Streaming 之前已经和大家分享过了,今天就和大家来聊一聊另一个特别重要的功能 -- CDC. CDC概述 何为CDC?Chang ...
- Spring Boot Admin 集成诊断利器 Arthas 实践
简介: Arthas 是 Alibaba 开源的 Java 诊断工具,具有实时查看系统的运行状况:查看函数调用参数.返回值和异常:在线热更新代码:秒解决类冲突问题:定位类加载路径:生成热点:通过网页诊 ...
- iLogtail 与Filebeat 性能对比
简介:前段时间, iLogtail 阿里千万实例可观测采集器开源,其中介绍了iLogtail采集性能可以达到单核100MB/s,相比开源采集Agent有5-10倍性能优势.很多小伙伴好奇iLogta ...
- [ELK] Elasticsearch 安装/配置、启动/停止、加节点/重启
Elasticsearch 在不同环境上支持的安装方式很多,有源码安装.二进制安装.docker安装.rpm包等管理器安装. 具体的,根据应用的实际情况选择即可. 完成可测试开发环境的建立后,需要进一 ...
- [Contract] web3.eth.getAccounts, web3.eth.getCoinbase 使用场景区别
web3.eth.getAccounts() 返回节点控制的账号列表(Promise returns Array) web3.eth.getCoinbase() 返回挖矿奖励所归集的地址(Promis ...
- 记联软 UniAccess 导致 NSIS 安装包启动进程失效
本文记录联软 UniAccess 注入的 C:\Window\LVUAAgentInstBaseRoot\syswow64\MozartBreathCore.dll 导致 NSIS 安装包启动进程失效 ...
- Vue3 和 Vue2 的异同及开发中具体区别
目录 总体来说 性能提升 树摇(Tree shaking) 碎片化节点(Fragment) 传送门 (Teleport) Suspense 更好的TypeScript支持 Composition AP ...
- css的animate做一个信号动画
html <div class="jump flex-fs fadeAndScaleIn"> <span></span> <span> ...
- linux安装nvm和node
linux安装nvm和node 一.环境 debian10 nodejs 二.安装 2.1 安装NVM 运行以下命令下载并运行 NVM 安装脚本: curl https://raw.githubuse ...
- 记录——Qt Qcreator 顶部菜单栏的隐藏与恢复
问题 我有一个朋友(嗯~无中生友),手残点击了 QCreator 中视图下的这个玩意儿: 当人的眼神不好时,可能不会看到这些快捷键以及无视这些弹窗. 解决方案 快捷键 ctrl + alt + M 可 ...