今天 遇到一个问题 访问一个接口数据 基本上是固定的,于是想把数据 缓存下来。。。于是版本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

最新文章

  1. iOS获取汉字的拼音
  2. winrar3.7-winrar4.0的注冊码
  3. Swift中类的两段式构造(类的构造过程)
  4. 黑马程序员_&lt;&lt;StringBuffer,包装类&gt;&gt;
  5. Struts2.3.34+Hibernate 4.x+Spring4.x 整合二部曲之下部曲
  6. [SDOI2008]烧水问题
  7. 一个很有趣的示例Spring Boot项目,使用Giraphe CMS和Spring Boot
  8. 简单Java类 全网最详细讲解 !!!
  9. JVM 调优参数解释
  10. jar包导入导出
  11. 用VSCode的debugger for chrome插件调试服务器项目的配置方式
  12. Android ------------------ 带边框的圆角矩形
  13. 重读《深入理解Java虚拟机》一、Java虚拟机内存区域的划分
  14. 9.Appium使用安装所需软件百度盘链接
  15. sql server 2012 打开提示无效的许可证数据。需要重新安装
  16. pip 安装包提速
  17. 性能监控-TP理解
  18. L1-029 是不是太胖了
  19. JS书籍推荐
  20. sql中in和exists的区别

热门文章

  1. Python的异步编程[0] -&gt; 协程[1] -&gt; 使用协程建立自己的异步非阻塞模型
  2. 区间DP【p2858】[USACO06FEB]奶牛零食Treats for the Cows
  3. 中文名: 交通事故责任认定, 英文名称: Traffic accident responsibility identification
  4. 【左偏树】HDU1512-Monkey King
  5. Exercise01_09
  6. zk介绍
  7. IE漏洞调试之CVE-2013-3893
  8. PHPCMS V9管理员password忘记怎样改动
  9. python抓取日本网站上iphone5的价格
  10. IntelliJ IDEA单元测试和代码覆盖率图解