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. Java工具篇之Disruptor高性能队列

    简介: disruptor适用于多个线程之间的消息队列,`作用与ArrayBlockingQueue有相似之处`,但是disruptor从功能.性能都远好于ArrayBlockingQueue,当多个 ...

  2. SysOM 案例解析:消失的内存都去哪了 !| 龙蜥技术

    简介: 这儿有一份"关于内存不足"排查实例,请查收. 文/系统运维 SIG 在<AK47 所向披靡,内存泄漏一网打尽>一文中,我们分享了slab 内存泄漏的排查方式和工 ...

  3. 揭秘 RocketMQ 新特性以及在金融场景下的实践

    2019 年末, RocketMQ 正式发布了 4.6.0 版本,增加了" Request-Reply "的同步调用的新特性." Request-Reply " ...

  4. 优化搜索排序结果从而“ 提升CTR、CVR业务指标”

    简介: 搭建搜索功能不难,难的是如何提高搜索质量,帮助用户快速找到心中所想的内容或商品,那么搜索结果的相关性排序则是影响用户体验最关键的一环,本文通过阿里云开放搜索电商行业解决方案和大家聊一聊如何优化 ...

  5. OpenYurt 开箱测评 | 一键让原生 K8s 集群具备边缘计算能力

    作者| 郑超 阿里云高级开发工程师 随着物联网技术以及 5G 技术的高速发展,将云计算的能力延伸至边缘设备端,并通过中心进行统一交付.管控,已成为云计算的重要发展趋势.为服务更多开发者把握这一趋势,5 ...

  6. [Caddy2] Caddyfile 静态文件托管 file_server 的 hide 用法

      file_server 语法: file_server [<matcher>] [browse] { root <path> hide <files...> i ...

  7. WEB服务与NGINX(13)-NGINX的日志功能

    1.nginx的日志功能 定义nginx的访问日志显示的格式,即具体记录的客户端信息和格式.日志功能由ngx_http_log_module模块提供. log_format name string . ...

  8. C数据结构:哈夫曼树算法实现与应用

    学习哈夫曼树(编码) 带权二叉树 认识WPL 最优二叉树 构造哈夫曼树的过程 哈夫曼树的应用 建立哈夫曼树 代码如下: 结构体代码部分 建立操作代码 找到最小结点(※难点) 附上建立哈夫曼树源代码 带 ...

  9. 基于Python实现MapReduce

    一.什么是MapReduce 首先,将这个单词分解为Map.Reduce. Map阶段:在这个阶段,输入数据集被分割成小块,并由多个Map任务处理.每个Map任务将输入数据映射为一系列(key, va ...

  10. sql 常用手册

    根据code 分组然后取出每组中的任意一行 SELECT *from( SELECT *, row_number () OVER ( partition BY code ORDER BY code D ...