缓存原理,自己写一个缓存类(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 ...
随机推荐
- Springboot使用ehcache缓存
本文部分步骤继承于springboot使用cache缓存,如果有不清楚的,请移驾springboot使用cache缓存 ehcache是一种广泛使用的开源Java分布式缓存.主要面向通用缓存,Java ...
- PostgreSQL CentOS 7 安装配置
https://www.postgresql.org/download/ 选择相应的版本 安装完成后,稍微配置下,否则无法远程访问: cd /var/lib/pgsql/11/data vi post ...
- STM32移植ROS--发布超声波信息
前言:之前ROS跟单片机的底层通讯主要是通过串口自定的协议来做,比如官网提供的arduino串口驱动一样,需要ROS往下发一个指令,单片机再回传一个指令,要写一大堆的协议,这样很麻烦,效率也比较低, ...
- 海边拾贝-G-若干有用的文章(乱序,经常更新)
若干有用的文章,乱序版本.会经常性修改. 若干Python模块的介绍不错 https://www.cnblogs.com/sui776265233/category/1239819.html ...
- not in和not exists区别
如果查询语句使用了not in 那么内外表都进行全表扫描,没有用到索引: 而not extsts 的子查询依然能用到表上的索引. 所以无论那个表大,用not exists都比not in要快. 也就是 ...
- java文件操作File类
1.文件路径操作 测试方法 @Test public void test5() { StringBuffer succBuffer = new StringBuffer("D:\\home\ ...
- 解决原生javascript 缺少insertAfter的功能,非Jquery方法
在现有的方法后插入一个新元素,你可能会想:既然有insertBefore方法,是不是也有一个相应的insertAfter()方法.很可惜,DOM没有提供方法.下面编写insertAfter函数,虽然D ...
- Kubernetes configMap(配置文件存储)
Kubernetes configMap(配置文件存储) 官方文档:https://kubernetes.io/docs/tasks/configure-pod-container/configure ...
- keepalived+Nginx实现主备保障Nginx的高可用。
1.什么是keepalived? Keepalived是集群管理中保证集群高可用的一个服务软件,用来防止单点故障. Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工 ...
- php中函数 isset(), empty(), is_null() 的区别,boolean类型和string类型的false判断
php中函数 isset(), empty(), is_null() 的区别,boolean类型和string类型的false判断 实际需求:把sphinx返回的结果放到ssdb缓存里,要考虑到sph ...