简单缓存Cache
接口
interface ICache
{
/// <summary>
/// 添加
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="expiratTime"></param>
void Add(string key, object value, int expiratTime = 30);
/// <summary>
/// 获取
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
T Get<T>(string key);
/// <summary>
/// 是否包含
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
bool Contains(string key);
/// <summary>
/// 删除指定key
/// </summary>
/// <param name="key"></param>
void Remove(string key);
/// <summary>
/// 删除所有
/// </summary>
void RemoveAll();
/// <summary>
/// 索引访问器
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
object this[string key] { get; set; }
/// <summary>
/// 数量
/// </summary>
int Count { get; }
}
实现类:
public class CustomerCache : ICache
{
private static Dictionary<string, KeyValuePair<object, DateTime>> _dictionary = new Dictionary<string, KeyValuePair<object, DateTime>>(); static CustomerCache()
{
new Action(() =>
{
while (true)
{
Thread.Sleep(100);
foreach (var item in _dictionary.Where(t => t.Value.Value < DateTime.Now))
{
_dictionary.Remove(item.Key);
}
}
}).BeginInvoke(null, null);
} public void Add(string key, object value, int expiratTime = 30)
{
KeyValuePair<object, DateTime> keyValue = new KeyValuePair<object, DateTime>(value, DateTime.Now.AddMinutes(expiratTime));
_dictionary[key] = keyValue;
} public bool Contains(string key)
{
if (_dictionary.ContainsKey(key))
{
KeyValuePair<object, DateTime> keyValue = _dictionary[key];
if (keyValue.Value > DateTime.Now)//没有过期
{
return true;
}
else
{
_dictionary.Remove(key);//过期清除
return false;
}
}
return false;
} public T Get<T>(string key)
{
if (_dictionary.ContainsKey(key))
{
KeyValuePair<object, DateTime> keyValue = _dictionary[key];
if (keyValue.Value > DateTime.Now)//没有过期
{
return (T)keyValue.Key;
}
else
{
_dictionary.Remove(key);//过期清除
return default(T);
}
}
return default(T);
} public void Remove(string key)
{
_dictionary.Remove(key);
} public void RemoveAll()
{
_dictionary = new Dictionary<string, KeyValuePair<object, DateTime>>();
} public object this[string key]
{
get
{
return this.Get<object>(key);
}
set
{
this.Add(key, value);
}
} public int Count
{
get
{
return _dictionary.Values.Where(t => t.Value > DateTime.Now).Count();
}
} }
缓存管理器:
public class CacheManager
{
private CacheManager()
{ } private static ICache cache = null; static CacheManager()
{
cache = (ICache)Activator.CreateInstance(typeof(CustomerCache));
} #region ICache
public static int Count
{
get { return cache.Count; }
} public static bool Contains(string key)
{
return cache.Contains(key);
} public static T Get<T>(string key)
{
return cache.Get<T>(key);
} public static void Add(string key, object value, int expiratTime = 30)
{
if (Contains(key))
cache.Remove(key);
cache.Add(key, value, expiratTime);
}
/// <summary>
/// 删除缓存数据项
/// </summary>
/// <param name="key"></param>
public static void Remove(string key)
{
cache.Remove(key);
} /// <summary>
/// 删除所有缓存数据项
/// </summary>
public static void RemoveAll()
{
cache.RemoveAll();
}
#endregion
}
简单缓存Cache的更多相关文章
- [.net 面向对象程序设计进阶] (15) 缓存(Cache)(二) 利用缓存提升程序性能
[.net 面向对象程序设计进阶] (15) 缓存(Cache)(二) 利用缓存提升程序性能 本节导读: 上节说了缓存是以空间来换取时间的技术,介绍了客户端缓存和两种常用服务器缓布,本节主要介绍一种. ...
- [.net 面向对象程序设计进阶] (14) 缓存(Cache) (一) 认识缓存技术
[.net 面向对象程序设计进阶] (14) 缓存(Cache)(一) 认识缓存技术 本节导读: 缓存(Cache)是一种用空间换时间的技术,在.NET程序设计中合理利用,可以极大的提高程序的运行效率 ...
- .Net自带缓存Cache的使用
对于数据比较大,经常要从数据库拿出来用的,可以考虑使用.Net自带的缓存Cache,简单好用: //向内存中插入一个缓存 System.Web.HttpRuntime.Cache.Insert(&qu ...
- 缓存Cache
转载自 博客futan 这篇文章将全面介绍有关 缓存 ( 互动百科 | 维基百科 )cache以及利用PHP写缓存caching的技术. 什么是缓存Cache? 为什么人们要使用它? 缓存 Cach ...
- ASP.NET缓存 Cache
缓存介绍 如果每次进入页面的时候都查询数据库生成页面内容的话,如果访问量非常大,则网站性能会非常差,而如果只有第一次访问的时候才查询数据库生成页面内容,以后都直接输出内容,则能提高系统性能,这样无论多 ...
- ASP.NET状缓存Cache的应用-提高数据库读取速度
原文:ASP.NET状缓存Cache的应用-提高数据库读取速度 一. Cache概述 既然缓存中的数据其实是来自数据库的,那么缓存中的数据如何和数据库进行同步呢?一般来说,缓存中应该存放改 ...
- 网页的缓存Cache与控制
什么是缓存 Cache? 缓存位于客户端与服务器之间, 或者服务器与服务器之间.它决定是否保存所获资源的副本,以及如何使用副本,何时更新副本,这里所说的资源包括页面的HTML, 图片,文件等等. 使用 ...
- jquery ui dialog弹出窗 清空缓存Cache或强制刷新
我用jquery ui 弹出一个购物车的对话,通过AJAX加载的数据.发现购物车被缓存,一直看到是旧数据.为了刷新购物车更新,我必须去加一个刷新按钮,点击后更新购物车页面.有没有一种方法来自动刷新加载 ...
- POCO库——Foundation组件之缓存Cache
缓存Cache:内部提供多种缓存Cache机制,并对不同机制的管理缓存策略不同实现: ValidArgs.h :ValidArgs有效键参数类,模板参数实现,_key:键,_isValid:是否有效, ...
随机推荐
- 单机Mongo复制集安装配置(数据库版本:4.x)
官方文档: https://docs.mongodb.com/manual/tutorial/deploy-replica-set-with-keyfile-access-control/#dep ...
- 通过唯一ID实现简单的日志跟踪实现
在实际项目中,通知我们需要记录一些日志,方便问题核查.但是日志多了就很容易混乱,请求,响应,执行中的日志无法对应,这时就需要为请求进行标记唯一ID来进行跟踪. /** * 记录请求日志 * * Cla ...
- HTTP——学习笔记(6)https
HTTP+加密+认证+完整性保护=HTTPS HTTP是一种新协议吗?: 不是,HTTPS只是HTTP通信接口部分用SSL和TLS协议代替而已 HTTP中,身处应用层的HTTP直接和TCP通信.而在使 ...
- 洛谷——P2615 神奇的幻方 【Noip2015 day1t1】
https://www.luogu.org/problem/show?pid=2615 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之 ...
- 微信开发出现 redirect-uri參数错误原因是设置回调页面域名不要加HTTP://
OAuth2.0 网页授权设置.回调页面域名不要加HTTP:// NND 微信的研发.你程序处理下非常麻烦吗?给个提示非常麻烦吗?让我查了1个多小时.
- bzoj1193: [HNOI2006]马步距离(贪心+bfs)
1193: [HNOI2006]马步距离 题目:传送门 题解: 毒瘤题... 模拟赛时的一道题,刚开始以为是一道大难题...一直在拼命找规律 结果.... 还是说正解吧: 暴力的解法肯定是直接bfs, ...
- What's the difference between returning void and returning a Task?
http://stackoverflow.com/questions/8043296/whats-the-difference-between-returning-void-and-returning ...
- nyoj--218--Dinner(语法)
Dinner 时间限制:100 ms | 内存限制:65535 KB 难度:1 描述 Little A is one member of ACM team. He had just won the ...
- 2.mongoDB 介绍(特点、优点、原理)
转自:https://www.cnblogs.com/hoojo/archive/2011/06/01/2066119.html 介绍:MongoDB是一个基于分布式文件存储的数据库.由C++语言编写 ...
- iview 分页的案例
//html部分 //js部分