.net中数据缓存使用
2024-09-19 10:30:20
今天 遇到一个问题 访问一个接口数据 基本上是固定的,于是想把数据 缓存下来。。。于是版本1 诞生了
private static ConcurrentDictionary<int, List<xxxxx>> xxxxCache = new ConcurrentDictionary<int, List<xxxxx>>(); private List<xxxxx> GetXXXByCache(int id)
{
if (!xxxxCache.ContainsKey(Id))
{
var list = xxxxService.GetxxxxList(Id);
xxxxCache.TryAdd(Id, list);
}
return xxxxxCache[Id];
}
跑起来没问题 然后 突然想到 一个问题 如果 接口数据变化 怎么办。。。。。。。于是想加了 缓存时间 发现 自己实现还要存过期时间 有点复杂 查查资料 发现net4.0有个 ObjectCache 于是 2.0 诞生了
private static ObjectCache DealerCache = MemoryCache.Default; private List<xxxxx> GetxxxByCache(int Id)
{
var cachekey = "xxxlist_" + Id;
var ret = Cache.Get(cachekey) as List<xxxx>;
if (ret == null)
{
ret = _xxxxService.GetxxxxList(Id);
var policy = new CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddMinutes() };
Cache.Set("list_" + Id, ret, policy);
}
return ret;
}
相关资料 :
http://www.cnblogs.com/TianFang/p/3430169.html
最新文章
- iOS获取汉字的拼音
- winrar3.7-winrar4.0的注冊码
- Swift中类的两段式构造(类的构造过程)
- 黑马程序员_<;<;StringBuffer,包装类>;>;
- Struts2.3.34+Hibernate 4.x+Spring4.x 整合二部曲之下部曲
- [SDOI2008]烧水问题
- 一个很有趣的示例Spring Boot项目,使用Giraphe CMS和Spring Boot
- 简单Java类 全网最详细讲解 !!!
- JVM 调优参数解释
- jar包导入导出
- 用VSCode的debugger for chrome插件调试服务器项目的配置方式
- Android ------------------ 带边框的圆角矩形
- 重读《深入理解Java虚拟机》一、Java虚拟机内存区域的划分
- 9.Appium使用安装所需软件百度盘链接
- sql server 2012 打开提示无效的许可证数据。需要重新安装
- pip 安装包提速
- 性能监控-TP理解
- L1-029 是不是太胖了
- JS书籍推荐
- sql中in和exists的区别
热门文章
- Python的异步编程[0] ->; 协程[1] ->; 使用协程建立自己的异步非阻塞模型
- 区间DP【p2858】[USACO06FEB]奶牛零食Treats for the Cows
- 中文名: 交通事故责任认定, 英文名称: Traffic accident responsibility identification
- 【左偏树】HDU1512-Monkey King
- Exercise01_09
- zk介绍
- IE漏洞调试之CVE-2013-3893
- PHPCMS V9管理员password忘记怎样改动
- python抓取日本网站上iphone5的价格
- IntelliJ IDEA单元测试和代码覆盖率图解