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 获取存储在缓存中的 ...
随机推荐
- 开箱即用!Linux 内核首个原生支持,让你的容器体验飞起来!| 龙蜥技术
简介: 本文将从 Nydus 架构回顾.RAFS v6 镜像格式和 EROFS over Fscache 按需加载技术三个角度来分别介绍这一技术的演变历程. 文/阿里云内核存储团队,龙蜥社区高性能存储 ...
- EasyNLP开源|中文NLP+大模型落地,EasyNLP is all you need
简介:EasyNLP背后的技术框架如何设计?未来有哪些规划?今天一起来深入了解. 作者 | 临在.岑鸣.熊兮 来源 | 阿里开发者公众号 一 导读 随着BERT.Megatron.GPT-3等预训练 ...
- 平台建设的7大问题:蚂蚁AI平台实践深度总结
简介: 在支持蚂蚁几乎所有核心业务运行和发展的过程中,我们在平台建设.业务支持.平台运营.AI创新以及AI整体运营等各个方面做了很多尝试,有了不少的收获和感悟,在此分享给大家. 过去几年,我和团队一直 ...
- 龙蜥利器:系统运维工具 SysAK的云上应用性能诊断 | 龙蜥技术
简介:本文从大量的性能诊断实践出发,来介绍 SysAK 在性能诊断上的方法论及相关工具. 文/张毅:系统运维SIG核心成员.SysAK 项目负责人:毛文安:系统运维 SIG 负责人. 系统运维既 ...
- 设计模式之装饰器-AOP
HelloWorld简单例子如下:此例子好好体会下继承 is a和组合 has a的异同. using System; using System.Runtime.InteropServices; na ...
- 012_DRC检查与处理
Check entire design:DRC检查整个原理图: Check Selection:DRC检查选择的部分电路: Use occurrences:选择所有事件进行检查: Use instan ...
- CF746 期望+逆序对
Link 题意:给定一个 \(1\) 到 \(n\) 的排列,等概率选一段区间 \([l, r]\) 随机排序,求期望逆序对数. \[E = \dfrac{\sum(cnt_{[1, n]} - cn ...
- Maven的概述
Maven的概述 @ 目录 Maven的概述 2. 依赖 3. Maven 的工作机制 3. 最后:感谢 Java 项目开发过程中,构建指的是使用『原材料生产产品』的过程. 原材料 Java 源代码 ...
- HZ2023 远足游记
你说得对,但是我放假之前写的 P4689 代码没了 所以来摆 4.6(远足) 上午 走路,刚开始感觉没啥 走到园博园发现没预料中那么顺利 但是还感觉没啥 因为也没预料到 \(N·m\) 学校会让我们原 ...
- 密钥存储在过时的 trusted.gpg 密钥环中(/etc/apt/trusted.gpg)
密钥存储在过时的 trusted.gpg 密钥环中(/etc/apt/trusted.gpg) 问题: 解决: cd /etc/opt sudo cp trusted.gpg trusted.gpg. ...