.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#版)的更多相关文章

  1. Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。

    #29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类T ...

  2. 分享:计算机图形学期末作业!!利用WebGL的第三方库three.js写一个简单的网页版“我的世界小游戏”

    这几天一直在忙着期末考试,所以一直没有更新我的博客,今天刚把我的期末作业完成了,心情澎湃,所以晚上不管怎么样,我也要写一篇博客纪念一下我上课都没有听,还是通过强大的度娘完成了我的作业的经历.(当然作业 ...

  3. Hibernate缓存原理与策略 Hibernate缓存原理:

    Hibernate缓存原理: 对于Hibernate这类ORM而言,缓存显的尤为重要,它是持久层性能提升的关键.简单来讲Hibernate就是对JDBC进行封装,以实现内部状态的管理,OR关系的映射等 ...

  4. springboot自动装配原理,写一个自己的start

    springboot自动装配原理 第一次使用springboot的时候,都感觉很神奇.只要加入一个maven的依赖,写几行配置,就能注入redisTemple,rabbitmqTemple等对象. 这 ...

  5. 如何写一个LaTeX类文件,并设计你自己的简历

    2017/8/29 20:26:03 原文地址 https://www.sharelatex.com/blog/2011/03/27/how-to-write-a-latex-class-file-a ...

  6. 请写一个java类,在任何时候都可以向它查询“你已经创建了多少个对象?”

    这个问题解决方法很简单,只要设置一个类的静态整型成员(事例中我设置的是n),初始化值为1,然后在其构造函数中添加语句使其+1(n++),这样需要查询创建了多少个对象时直接查询n的值就可以了,如下: p ...

  7. 29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。

    //Vehicle类 package d922A; public class Vehicle { private int wheels; private double weight; Vehicle( ...

  8. 随便写一个c++类

    为了让代码更贴合实际项目需要,我们分别用xxx.h文件,xxx.cpp文件来包含类的定义,类的声明和类的调用部分,实验平台vs2010 mycoach.h文件 #pragma once #includ ...

  9. 编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。

    package car; public class Vehicle { //定义成员变量 private int wheels; private double weight; public int g ...

随机推荐

  1. github clone加速

    1. 在https://asm.ca.com/zh_cn/ping.php 网址中查询 github.global.ssl.fastly.net 及 github.com 的 china地区 avr ...

  2. vue-cli安装以及创建一个简单的项目(二)(vuex使用、发行一个简单的app)

    1.vuex的使用 vuex是vue的状态管理中心,vuex来保存我们需要管理的状态值,值一旦被修改,所有引用该值的地方就会自动更新,常用于: 1.多个视图依赖同一状态(l例:菜单导航) 2.来自不同 ...

  3. ssh工具推荐MobaXterm 可能是你遇到过的比较出色的一款

    之前一直用xshell,现在推荐一个更好用的工具. 一站式的解决你的需求,而且画风个人也比较喜欢,而且随便一百度就能找得到green PJ 的版本

  4. 使用角色管理工具 安装或配置microsoft.net framework 3.5 sp1

    解决方法:

  5. Centos下mysql8忘记root密码的解决办法

    首先,打开配置文件/etc/my.cnf,在末尾添加一行: skip-grant-tables 然后重启mysql服务: service mysqld restart 然后可以直接登录到mysql,在 ...

  6. NIO你真正了解多少?

    解释一下java.io.Serializable接口 类通过实现 Java.io.Serializable 接口以启用其序列化功能.未实现此接口的类将无法使其任何状态序列化或反序列化. IO操作最佳实 ...

  7. 夜神模拟器怎么连接adb

    夜神模拟器连接不了adb的原因主要是adb的版本与夜神模拟器adb版本不一致造成的,具体的解决办法请看下面的操作步骤. 工具/原料   电脑 安装了夜神模拟器 方法/步骤   1 使用快捷键win + ...

  8. Mysql、Oracle、SQLServer等数据库参考文档免费分享下载

    场景 MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统 ...

  9. 十一:外观模式详解(Service,action与dao)

    定义:外观模式是软件工程中常用的一种软件设计模式.它为子系统中的一组接口提供一个统一的高层接口.这一接口使得子系统更加容易使用. 该定义引自百度百科,它的表现很简单,将一系列子接口的功能进行整理,从而 ...

  10. String.trim()源码解析

    trim()这个方法一般用来消除字符串两边的空格,但是内部是如何实现的呢? 附上源码: public String trim() { int len = value.length; int st = ...