接口

 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
}

  

最新文章

  1. JS 循环给li绑定参数不同的点击事
  2. [POJ2151]Check the difficulty of problems (概率dp)
  3. Func&lt;T&gt;、Action&lt;T&gt; 的区别于说明
  4. UrlRewriter.dll伪静态实现二级域名泛解析
  5. IOS webview中cookie的读取与保存-b
  6. Qt入门(10)——调试技术
  7. axure RP Pro7.0加载日历控件的步骤
  8. HDU 4611Balls Rearrangement(思维)
  9. php键值相同的项数值相加
  10. Spring源码解析——如何阅读源码
  11. beanshell postprocessor解决编码
  12. 【python】type()、instance()
  13. linux 下检查java jar包 程序是否正常 shell
  14. CSS绝对定位元素居中的几种方法
  15. 【PAT】A1034Head of a Gang
  16. Ubuntu 16.04下安装MySQL及远程连接
  17. (转)The C10K problem翻译
  18. IEEE 754二进制浮点数算术标准
  19. Linux Centos7 虚拟机上网设置
  20. c++ vector init操作

热门文章

  1. visio使用技巧
  2. CSDN博客给我带来的一些诱惑和选择机会
  3. Postman(API &amp; HTTP请求调试插件)
  4. BootStrap有用代码片段(持续总结)
  5. Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.GINGERBREAD
  6. 4. idea常用快捷键设置(改为eclipse相似)
  7. form表单系列中文件上传及预览
  8. 单表的更新UPDATE和删除记录DELETE(二十六)
  9. java中&quot;&quot;.equals(A)与A.equals(&quot;&quot;)一样不?
  10. PostgreSQL Replication之第七章 理解Linux高可用(1)