1、分布式缓存—Redis与Memched的区别redis
1.一、 数据支持类型缓存
1.二、 持久性并发
1.三、 内存利用状况分布式
1.四、 数据一致性性能
1.五、 性能区别spa
1.六、 总结线程
2、内存缓存—Cache(.Net Core)code
2.一、 介绍适用场景对象
2.二、 提供的过时的方式blog
2.三、 如何使用
1、分布式缓存—Redis与Memched的区别
1.一、 数据支持类型
Redis:支持String(字符串)、Hash(哈希)、List(列表)、Set(集合)、 ZSet(有序集合)、Bitmaps(位图)、HyperLogLog、Geo(地理信息定位)
Memched:简单的key/value数据类型
1.二、 持久性
Redis:Redis经过 RDB与AOF持久化,能够将内存中的数据保存到硬盘中,而后重启以后在读取数据
Memched:不支持数据的持久性的存储
1.三、 内存利用状况
使用简单的key-value存储的话,Memcached的内存利用率更高,而若是Redis采用hash结构来作key-value存储,因为其组合式的压缩,其内存利用率会高于Memcached。
Memcached默认使用Slab Allocation机制管理内存,主要思想是按照原先预约的大小分配内存大小,当客户端发过来数据的时候会选择一个最适合的地方给它储存,,好处是效率高,
不会形成内存碎片,可是很差的一点就是分配的内存大小124字节,传进来的大小是100字节,那么24字节的内存也就浪费了。
1.四、 数据一致性
Redis:单线程保证了数据的顺序,同时redis还有事务操做
Memcached:memcache须要使用cas保证数据一致性。CAS(Check and Set)是一个确保并发一致性的机制,属于“乐观锁”范畴;原理很简单:拿版本号,操做,对比版本号,
若是一致就操做,不一致就放弃任何操做
1.五、 性能区别
Redis使用单核,Memcached可使用多核,因此在处理小的文件的时候Redis会比Memcached有更高的效率,可是在100KB以上的时候,Memcached的效率就会比Redis更高一点
1.六、 总结
以上就是Redis和Memcached大体的比较了。各有各的优势以及缺点,存在即合理,只有在使用在合适的运用场景,才是最有效率的。
2、内存缓存—Cache(.Net Core)
2.一、介绍适用场景
Cache,中译名高速缓冲存储器,其做用是为了更好的利用局部性原理,减小CPU访问主存的次数。简单地说,CPU正在访问的指令和数据,其可能会被之后屡次访问到,或者是该指令和数据附近的内存区域,也可能会被屡次访问。所以,第一次访问这一块区域时,将其复制到Cache中,之后访问该区域的指令或者数据时,就不用再从主存中取出。
内存缓存能够存储任何对象; 分布式缓存接口仅限于byte[]
。内存和分布式缓存将缓存项存储为键值对。
2.二、提供的过时的方式
主要介绍两种:
2.三、如何使用
新建一个CacheHelper公共类
public class CacheHelper { private static IMemoryCache _memoryCache; public CacheHelper(IMemoryCache memoryCache) { _memoryCache = memoryCache; } /// <summary> /// 建立绝对过时时间缓存 /// </summary> /// <param name="cacheKey">缓存key</param> /// <param name="obj">缓存对象</param> /// <param name="expireDate">过时时间(绝对)分钟</param> public static void SetAbsolute(string cacheKey, object obj,int expireDate= 10 * 60) { //绝对到期时间 var cacheEntryOptions = new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(expireDate)); _memoryCache.Set(cacheKey, obj, cacheEntryOptions); } /// <summary> /// 每隔多长时间不调用就让其过时 /// </summary> /// <param name="cacheKey">缓存key</param> /// <param name="obj">缓存对象</param> /// <param name="expireDate">过时时间(访问缓存重置时间)</param> public static void SetSliding(string cacheKey, object obj, int expireDate= 10 * 60) { //绝对到期时间 var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(expireDate)); _memoryCache.Set(cacheKey, obj, cacheEntryOptions); } /// <summary> /// 判断缓存是否存在 /// </summary> /// <param name="key">缓存key</param> /// <returns></returns> public static bool IsExist(string cacheKey) { if (string.IsNullOrWhiteSpace(cacheKey)) { return false; } return _memoryCache.TryGetValue(cacheKey, out _); } /// <summary> /// 获取缓存对象 /// </summary> /// <param name="cacheKey">缓存key</param> /// <returns>object对象</returns> public static object Get(string cacheKey) { if (string.IsNullOrEmpty(cacheKey)) { return null; } return _memoryCache.Get(cacheKey); } /// <summary> /// 获取缓存对象 /// </summary> /// <typeparam name="T">T对象</typeparam> /// <param name="cacheKey">缓存Key</param> /// <returns></returns> public static T Get<T>(string cacheKey) { if (string.IsNullOrEmpty(cacheKey)) { return default(T); } if (!_memoryCache.TryGetValue<T>(cacheKey, out T cacheEntry)) { return default(T); } return cacheEntry; } /// <summary> /// 获取全部缓存键 /// </summary> /// <returns></returns> public static List<string> GetCacheKeys() { const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic; var entries = _memoryCache.GetType().GetField("_entries", flags).GetValue(_memoryCache); var cacheItems = entries as IDictionary; var keys = new List<string>(); if (cacheItems == null) return keys; foreach (DictionaryEntry cacheItem in cacheItems) { keys.Add(cacheItem.Key.ToString()); } return keys; } /// <summary> /// 移除指定数据缓存 /// </summary> /// <param name="cacheKey">缓存key</param> public static void RemoveCache(string cacheKey) { _memoryCache.Remove(cacheKey); } /// <summary> /// 移除所有缓存 /// </summary> public static void RemoveAllCache() { var keysList = GetCacheKeys(); foreach (string key in keysList) { _memoryCache.Remove(key); } } }
而后根据前两篇文章所讲的依赖注入在Startup.cs里面注册
services.AddMemoryCache();
services.AddSingleton<CacheHelper>();
而后就能够正常使用了。
CacheHelper.SetAbsolute("admin", admin, 10 * 60);