简单缓存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:是否有效, ...
随机推荐
- python 比较数字大小按从大到小输出
主要用到的python 的知识点 1: 内置函数max 2: 列表的操作 3: while 循环 4 : 错误处理 代码如下: #!/usr/bin/python #coding=u ...
- yum下载的rpm包离线安装
#修改yum设置,让rpm包缓存到本地 vi /etc/yum.conf #修改keepcache为1 keepcache=1 #清空yum缓存 yum clean all #安装你要离线安装的rpm ...
- 洛谷 P2665 [USACO08FEB]连线游戏Game of Lines
P2665 [USACO08FEB]连线游戏Game of Lines 题目背景 Farmer John最近发明了一个游戏,来考验自命不凡的贝茜. 题目描述 Farmer John has chall ...
- Qt资料大全
简述 发福利了.发福利了.发福利了,重要的事情说三遍... 为了方便更多Qter了解.学习Qt,现将相关资源进行整理,主要内容包括:Qt官网.编码风格.GitHub & Third-Party ...
- A. Ilya and Diplomas( Codeforces Round #311 (Div. 2) )
A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...
- linux 抓包 tcpdump 简单应用
在linuxserver上,常常要定位网络问题,就须要用到抓包. 比如:tcpdump -X -s 0 host 10.17.81.22 and port 9999 -w /home/text.cap ...
- 基于UDP的DDos反射放大攻击
转自:https://www.us-cert.gov/ncas/alerts/TA14-017A Protocol Bandwidth Amplification Factor DNS 28 to 5 ...
- hdoj--3552--I can do it!(贪心模拟)
I can do it! Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Tot ...
- centos7 阿里云yum源更换
个人比较喜欢阿里云yum源,同时使用centos7 首先 cd /etc/yum.repos.d/ wget -O /etc/yum.repos.d/CentOS-Base.repo http://m ...
- POJ 3204 网络流的必须边
思路: 求一遍网络流 在残余网络上DFS 从起点DFS 从终点把边反向DFS 一个边跟起点连通 跟终点反向的边连通 ans++ 注:此题不能用tarjan 因为有边权为0的边 //By SiriusR ...