简单缓存Cache
2024-09-07 17:08:05
接口
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
}
最新文章
- JS 循环给li绑定参数不同的点击事
- [POJ2151]Check the difficulty of problems (概率dp)
- Func<;T>;、Action<;T>; 的区别于说明
- UrlRewriter.dll伪静态实现二级域名泛解析
- IOS webview中cookie的读取与保存-b
- Qt入门(10)——调试技术
- axure RP Pro7.0加载日历控件的步骤
- HDU 4611Balls Rearrangement(思维)
- php键值相同的项数值相加
- Spring源码解析——如何阅读源码
- beanshell postprocessor解决编码
- 【python】type()、instance()
- linux 下检查java jar包 程序是否正常 shell
- CSS绝对定位元素居中的几种方法
- 【PAT】A1034Head of a Gang
- Ubuntu 16.04下安装MySQL及远程连接
- (转)The C10K problem翻译
- IEEE 754二进制浮点数算术标准
- Linux Centos7 虚拟机上网设置
- c++ vector init操作
热门文章
- visio使用技巧
- CSDN博客给我带来的一些诱惑和选择机会
- Postman(API &; HTTP请求调试插件)
- BootStrap有用代码片段(持续总结)
- Build.VERSION.SDK_INT >;= Build.VERSION_CODES.GINGERBREAD
- 4. idea常用快捷键设置(改为eclipse相似)
- form表单系列中文件上传及预览
- 单表的更新UPDATE和删除记录DELETE(二十六)
- java中";";.equals(A)与A.equals(";";)一样不?
- PostgreSQL Replication之第七章 理解Linux高可用(1)