缓存原理,自己写一个缓存类(c#版)
.net中的MemoryCache是通过内部封装一个静态Dictionary
自己写一个缓存,来看看内部怎么实现的
public class CustomerCache : ICache
{ private static ConcurrentDictionary<string, KeyValuePair<DateTime, object>> _CustomerCacheDictionary = new ConcurrentDictionary<string, KeyValuePair<DateTime, object>>(); static CustomerCache()
{
Task.Run(() =>
{
while (true)
{
if (_CustomerCacheDictionary.Count > )
{
List<string> list = new List<string>();
foreach (var item in _CustomerCacheDictionary)
{
KeyValuePair<DateTime, object> keyValuePair = item.Value;
if (DateTime.Now > keyValuePair.Key)//过期了
{
list.Add(item.Key);
}
}
foreach (var key in list)
{
_CustomerCacheDictionary.TryRemove(key,out var v);
}
}
Thread.Sleep();
}
});
} /// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="data"></param>
/// <param name="cacheTime">默认30分钟 单位分钟</param>
public void Add(string key, object data, int cacheTime = )
{
if (_CustomerCacheDictionary.ContainsKey(key))
throw new Exception("相同的key");
_CustomerCacheDictionary.TryAdd(key, new KeyValuePair<DateTime, object>(DateTime.Now.AddMinutes(cacheTime), data));
} /// <summary>
/// 应该先检查,再get
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public T Get<T>(string key)
{
return (T)_CustomerCacheDictionary[key].Value;
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Contains(string key)
{
if (_CustomerCacheDictionary.ContainsKey(key))
{
KeyValuePair<DateTime, object> keyValuePair = _CustomerCacheDictionary[key];
if (DateTime.Now > keyValuePair.Key)//过期了
{
_CustomerCacheDictionary.TryRemove(key,out var v);
return false;
}
else
return true;
}
return false;
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="func">真实查询数据源的方法</param>
/// <returns></returns>
public T GetT<T>(string key, Func<T> func)
{
T t;
if (!this.Contains(key))
{
t = func.Invoke();
this.Add(key, t);
}
else
{
t = this.Get<T>(key);
}
return t;
} public object this[string key] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } public int Count {
get {
return _CustomerCacheDictionary.Count;
}
} public void Remove(string key)
{
_CustomerCacheDictionary.TryRemove(key, out var v);
} public void RemoveAll()
{
_CustomerCacheDictionary.Clear();
}
}
public interface ICache
{
/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
T Get<T>(string key); /// <summary>
/// Adds the specified key and object to the cache.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
/// <param name="cacheTime">Cache time</param>
void Add(string key, object data, int cacheTime = ); /// <summary>
/// Gets a value indicating whether the value associated with the specified key is cached
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
bool Contains(string key); /// <summary>
/// Removes the value with the specified key from the cache
/// </summary>
/// <param name="key">/key</param>
void Remove(string key); /// <summary>
/// Clear all cache data
/// </summary>
void RemoveAll(); object this[string key] { get; set; } int Count { get; }
}
缓存原理,自己写一个缓存类(c#版)的更多相关文章
- Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。
#29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类T ...
- 分享:计算机图形学期末作业!!利用WebGL的第三方库three.js写一个简单的网页版“我的世界小游戏”
这几天一直在忙着期末考试,所以一直没有更新我的博客,今天刚把我的期末作业完成了,心情澎湃,所以晚上不管怎么样,我也要写一篇博客纪念一下我上课都没有听,还是通过强大的度娘完成了我的作业的经历.(当然作业 ...
- Hibernate缓存原理与策略 Hibernate缓存原理:
Hibernate缓存原理: 对于Hibernate这类ORM而言,缓存显的尤为重要,它是持久层性能提升的关键.简单来讲Hibernate就是对JDBC进行封装,以实现内部状态的管理,OR关系的映射等 ...
- springboot自动装配原理,写一个自己的start
springboot自动装配原理 第一次使用springboot的时候,都感觉很神奇.只要加入一个maven的依赖,写几行配置,就能注入redisTemple,rabbitmqTemple等对象. 这 ...
- 如何写一个LaTeX类文件,并设计你自己的简历
2017/8/29 20:26:03 原文地址 https://www.sharelatex.com/blog/2011/03/27/how-to-write-a-latex-class-file-a ...
- 请写一个java类,在任何时候都可以向它查询“你已经创建了多少个对象?”
这个问题解决方法很简单,只要设置一个类的静态整型成员(事例中我设置的是n),初始化值为1,然后在其构造函数中添加语句使其+1(n++),这样需要查询创建了多少个对象时直接查询n的值就可以了,如下: p ...
- 29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。
//Vehicle类 package d922A; public class Vehicle { private int wheels; private double weight; Vehicle( ...
- 随便写一个c++类
为了让代码更贴合实际项目需要,我们分别用xxx.h文件,xxx.cpp文件来包含类的定义,类的声明和类的调用部分,实验平台vs2010 mycoach.h文件 #pragma once #includ ...
- 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。
package car; public class Vehicle { //定义成员变量 private int wheels; private double weight; public int g ...
随机推荐
- PMP图表(必背)
- golang基础之第一个go程序
编写 Hello World 创建文件 hello.go,不写入任何内容.按照如下的命令尝试进行编译 $ go run hello.go 将会打印出如下错误: package main: hello. ...
- 小小见解之python循环依赖
a.py from b import b print '---------this is module a.py----------' def a(): print "hello, a&qu ...
- shell脚本语言与linux命令的联系与区别
使用linux肯定是要会使用命令的,就算提供有用户界面,绝大部分功能还是要通过命令行去操作的.而shell脚本语言也是运行在linux上的脚本语言,对于服务器运维人员也是几乎必须要掌握的.而shell ...
- Node.js能解决什么问题?
一.使用Node.js能解决什么问题 对于PHP.JAVA.Python等服务端语言中,为每个客户端连接创建一个新的线程,而每个线程需要大约2M的内存,理论上,具有8GB内存的服务器可以同时连接的最大 ...
- C# winform打开新窗体显示一段时间 关闭新窗体
1.form1的button事件下: form2 form = new form2(); form.Show(); Thread.Sleep(10000); //form2窗体显示10秒 form. ...
- HTTPS免费证书
HTTPS 证书 一: 利用 cerbot 本地服务器 申请免费可用的证书.缺点就是三个月需要手动换一次 官网: https://certbot.eff.org/lets-encrypt/ubuntu ...
- Pytest 测试框架
一 . Pytest 简介 Pytest是python的一种单元测试框架. 1. pytest 特点 入门简单,文档丰富 支持单元测试,功能测试 支持参数化,重复执行,部分执行,测试跳过 兼容其他测试 ...
- switch case加范围判断
switch case是可以加范围判断的,但是语法有少许变化,参数不能写在switch里面,而是写在外面,如: const i = 3; switch (true) { case (i <= 0 ...
- 基于Proxy的小程序状态管理
摘要: 小程序状态管理. 作者:wwayne 原文:基于Proxy的小程序状态管理 Fundebug经授权转载,版权归原作者所有. 微信小程序的市场在进一步的扩大,而背后的技术社区仍在摸索着最好的实践 ...