首页 > 数据库 >.NET 6 基于IDistributedCache实现Redis与MemoryCache的缓存帮助类

.NET 6 基于IDistributedCache实现Redis与MemoryCache的缓存帮助类

时间:2022-12-05 18:12:32浏览次数:46  
标签:缓存 string MemoryCache IDistributedCache Redis value cache key public

本文通过IDistributedCache的接口方法,实现RedisMemoryCache统一帮助类。只需要在配置文件中简单的配置一下,就可以实现Redis与MemoryCache的切换。

目录

IDistributedCache

IDistributedCache 方法:

方法 说明
Get(String) 获取具有给定键的值。
GetAsync(String, CancellationToken) 获取具有给定键的值。
Refresh(String) 基于缓存中某个值的键刷新该值,并重置其可调到期超时(如果有)。
RefreshAsync(String, CancellationToken) 基于缓存中某个值的键刷新该值,并重置其可调到期超时(如果有)。
Remove(String) 删除具有给定键的值。
RemoveAsync(String, CancellationToken) 删除具有给定键的值。
Set(String, Byte[], DistributedCacheEntryOptions) 设置具有给定键的值。
SetAsync(String, Byte[], DistributedCacheEntryOptions, CancellationToken) 设置具有给定键的值。

IDistributedCache 还提供了一些扩展方法,本文的帮助类就是通过扩展方法完成的。

IDistributedCache 扩展方法:

方法 说明
GetString(IDistributedCache, String) 使用指定的键从指定的缓存中获取字符串。
GetStringAsync(IDistributedCache, String, CancellationToken) 使用指定的键从指定的缓存异步获取字符串。
Set(IDistributedCache, String, Byte[]) 使用指定的键设置指定缓存中的字节序列。
SetAsync(IDistributedCache, String, Byte[], CancellationToken) 使用指定的键异步设置指定缓存中的字节序列。
SetString(IDistributedCache, String, String) 使用指定的键在指定的缓存中设置字符串。
SetString(IDistributedCache, String, String, DistributedCacheEntryOptions) 使用指定的键在指定的缓存中设置字符串。
SetStringAsync(IDistributedCache, String, String, DistributedCacheEntryOptions, CancellationToken) 使用指定的键在指定的缓存中异步设置字符串。
SetStringAsync(IDistributedCache, String, String, CancellationToken) 使用指定的键在指定的缓存中异步设置字符串。

ICache 接口

ICache接口提供了设置缓存、获取缓存、删除缓存和刷新缓存的接口方法。



namespace CacheHelper
{
    public interface ICache
    {
        #region 设置缓存 
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">值</param>
        void SetCache(string key, object value);
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">值</param>
        Task SetCacheAsync(string key, object value);

        /// <summary>
        /// 设置缓存
        /// 注:默认过期类型为绝对过期
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">值</param>
        /// <param name="timeout">过期时间间隔</param>
        void SetCache(string key, object value, TimeSpan timeout);

        /// <summary>
        /// 设置缓存
        /// 注:默认过期类型为绝对过期
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">值</param>
        /// <param name="timeout">过期时间间隔</param>
        Task SetCacheAsync(string key, object value, TimeSpan timeout);

        /// <summary>
        /// 设置缓存
        /// 注:默认过期类型为绝对过期
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">值</param>
        /// <param name="timeout">过期时间间隔</param>
        /// <param name="expireType">过期类型</param>  
        void SetCache(string key, object value, TimeSpan timeout, ExpireType expireType);

        /// <summary>
        /// 设置缓存
        /// 注:默认过期类型为绝对过期
        /// </summary>
        /// <param name="key">缓存Key</param>
        /// <param name="value">值</param>
        /// <param name="timeout">过期时间间隔</param>
        /// <param name="expireType">过期类型</param>  
        Task SetCacheAsync(string key, object value, TimeSpan timeout, ExpireType expireType);
        #endregion

        #region 获取缓存

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        string GetCache(string key);

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        Task<string> GetCacheAsync(string key);
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        T GetCache<T>(string key);
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        Task<T> GetCacheAsync<T>(string key);

        #endregion

        #region 删除缓存

        /// <summary>
        /// 清除缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        void RemoveCache(string key);

        /// <summary>
        /// 清除缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        Task RemoveCacheAsync(string key);

        #endregion

        #region 刷新缓存
        /// <summary>
        /// 刷新缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        void RefreshCache(string key);
        /// <summary>
        /// 刷新缓存
        /// </summary>
        /// <param name="key">缓存Key</param>
        Task RefreshCacheAsync(string key);
        #endregion
    }
}

ExpireType枚举

ExpireType枚举标识缓存的过期类型,分为绝对过期相对过期两个类型。
绝对过期:即自创建一段时间后就过期
相对过期:即该键未被访问后一段时间后过期,若此键一直被访问则过期时间自动延长。

namespace CacheHelper
{
    public enum ExpireType
    {
        /// <summary>
        /// 绝对过期
        /// 注:即自创建一段时间后就过期
        /// </summary>
        Absolute,

        /// <summary>
        /// 相对过期
        /// 注:即该键未被访问后一段时间后过期,若此键一直被访问则过期时间自动延长
        /// </summary>
        Relative,
    }
}

CacheType 枚举

是使用MemoryCache,还是RedisMemoryCache不支持分布式,Redis支持分布式。

namespace CacheHelper
{
    public enum CacheType
    {
        /// <summary>
        /// 使用内存缓存(不支持分布式)
        /// </summary>
        Memory,

        /// <summary>
        /// 使用Redis缓存(支持分布式)
        /// </summary>
        Redis
    }
}

CacheHelper 缓存帮助类

namespace CacheHelper
{
    public class CacheHelper : ICache
    {
        readonly IDistributedCache _cache;

        public CacheHelper(IDistributedCache cache)
        {
            _cache = cache;
        }

        protected string BuildKey(string idKey)
        {
            return $"Cache_{GetType().FullName}_{idKey}";
        }
        public void SetCache(string key, object value)
        {
            string cacheKey = BuildKey(key);
            _cache.SetString(cacheKey, value.ToJson());
        }

        public async Task SetCacheAsync(string key, object value)
        {
            string cacheKey = BuildKey(key);
            await _cache.SetStringAsync(cacheKey, value.ToJson());
        }

        public void SetCache(string key, object value, TimeSpan timeout)
        {
            string cacheKey = BuildKey(key);
            _cache.SetString(cacheKey, value.ToJson(), new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = new DateTimeOffset(DateTime.Now + timeout)
            });
        }

        public async Task SetCacheAsync(string key, object value, TimeSpan timeout)
        {
            string cacheKey = BuildKey(key);
            await _cache.SetStringAsync(cacheKey, value.ToJson(), new DistributedCacheEntryOptions
            {
                AbsoluteExpiration = new DateTimeOffset(DateTime.Now + timeout)
            });
        }

        public void SetCache(string key, object value, TimeSpan timeout, ExpireType expireType)
        {
            string cacheKey = BuildKey(key);
            if (expireType == ExpireType.Absolute)
            {
                //这里没转换标准时间,Linux时区会有问题?
                _cache.SetString(cacheKey, value.ToJson(), new DistributedCacheEntryOptions
                {
                    AbsoluteExpiration = new DateTimeOffset(DateTime.Now + timeout)
                });
            }
            else
            {
                _cache.SetString(cacheKey, value.ToJson(), new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = timeout
                });
            }
        }

        public async Task SetCacheAsync(string key, object value, TimeSpan timeout, ExpireType expireType)
        {
            string cacheKey = BuildKey(key);
            if (expireType == ExpireType.Absolute)
            {
                //这里没转换标准时间,Linux时区会有问题?
                await _cache.SetStringAsync(cacheKey, value.ToJson(), new DistributedCacheEntryOptions
                {
                    AbsoluteExpiration = new DateTimeOffset(DateTime.Now + timeout)
                });
            }
            else
            {
                await _cache.SetStringAsync(cacheKey, value.ToJson(), new DistributedCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = timeout
                });
            }
        }

        public string GetCache(string idKey)
        {
            if (idKey.IsNullOrEmpty())
            {
                return null;
            }
            string cacheKey = BuildKey(idKey);
            var cache = _cache.GetString(cacheKey);
            return cache;
        }
        public async Task<string> GetCacheAsync(string key)
        {
            if (key.IsNullOrEmpty())
            {
                return null;
            }
            string cacheKey = BuildKey(key);
            var cache = await _cache.GetStringAsync(cacheKey);
            return cache;
        }

        public T GetCache<T>(string key)
        {
            var cache = GetCache(key);
            if (!cache.IsNullOrEmpty())
            {
                return cache.ToObject<T>();
            }
            return default(T);
        }

        public async Task<T> GetCacheAsync<T>(string key)
        {
            var cache = await GetCacheAsync(key);
            if (!string.IsNullOrEmpty(cache))
            {
                return cache.ToObject<T>();
            }
            return default(T);
        }

        public void RemoveCache(string key)
        {
            _cache.Remove(BuildKey(key));
        }

        public async Task RemoveCacheAsync(string key)
        {
            await _cache.RemoveAsync(BuildKey(key));
        }

        public void RefreshCache(string key)
        {
            _cache.Refresh(BuildKey(key));
        }

        public async Task RefreshCacheAsync(string key)
        {
            await _cache.RefreshAsync(BuildKey(key));
        }

    }
}

CacheHelper 中,自定义了一个string的扩展方法ToObject<T>()ToObject<T>()扩展方法使用了 Newtonsoft.Json

/// <summary>
/// 将Json字符串反序列化为对象
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="jsonStr">Json字符串</param>
/// <returns></returns>
public static T ToObject<T>(this string jsonStr)
{
    return JsonConvert.DeserializeObject<T>(jsonStr);
}

CacheHelper 的使用方法

安装Redis依赖

Redis依赖我使用的是Caching.CSRedis,安装依赖:

PM> Install-Package Caching.CSRedis -Version 3.6.90

配置appsettings.json

在appsettings.json中,对缓存进行配置:

 "Cache": {
    "CacheType": "Memory", // "Memory OR Redis"
    "RedisEndpoint": "127.0.0.1:6379" //Redis节点地址,定义详见 https://github.com/2881099/csredis
  },

如果要使用MemoryCache,CacheType就设置为Memory,如果要使用Redis,CacheType就设置为Redis。如果设置为Redis的话,还需要配置RedisEndpoint,保证Redis节点可用。

CacheOptions配置

编写一个名为CacheOptions的类。用于获取配置文件的配置节内容

namespace CacheHelper
{
    public class CacheOptions
    {
        public CacheType CacheType { get; set; }
        public string RedisEndpoint { get; set; }
    }
}

IHostBuilder扩展方法UseCache

编写一个IHostBuilder的扩展方法UseCache,用于注入MemoryCache或是Redis

public static IHostBuilder UseCache(this IHostBuilder hostBuilder)
{
    hostBuilder.ConfigureServices((buidlerContext, services) =>
    {
        var cacheOption = buidlerContext.Configuration.GetSection("Cache").Get<CacheOptions>();
        switch (cacheOption.CacheType)
        {
            case CacheType.Memory: services.AddDistributedMemoryCache(); break;
            case CacheType.Redis:
                {
                    var csredis = new CSRedisClient(cacheOption.RedisEndpoint);
                    RedisHelper.Initialization(csredis);
                    services.AddSingleton(csredis);
                    services.AddSingleton<IDistributedCache>(new CSRedisCache(RedisHelper.Instance));
                }; break;
            default: throw new Exception("缓存类型无效");
        }
    });

    return hostBuilder;
}

Program.cs中引用

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseCache();

CacheHelper的使用。

public class HomeController
{
	readonly ICache _cache;
	public HomeController
	(
		ICache cache,
	)
	{
		_cache = cache;
	}
	
	public async Task CacheTest(string key)
	{
		string cache_value = "hello cache";
		//同步方法
		_cache.SetCache(key,cache_value );
		string v = _cache.GetCache<string>(key);
		_cache.RemoveCache(key);
		//异步方法
		await _cache.SetCacheAsync(key,cache_value );
		string val = await _cache.GetCacheAsync<string>(key);
		await _cache.RemoveCacheAsync(key);
	}
}

总结

暂无,下次再会!


标签:缓存,string,MemoryCache,IDistributedCache,Redis,value,cache,key,public
From: https://www.cnblogs.com/gmval/p/16953063.html

相关文章

  • Redis配置、优化以及命令
    一、关系数据库和非关系型数据库1、关系型数据库关系型数据库是一个结构化的数据库,创建在关系模型(二维表格模型)基础上,一般面向于记录。SQL语句(标准数据查询语言)就是一种......
  • redis底层数据结构总结
    hash:是一维数组加链表 ziplink:压缩列表相当于数组,链表查询速度快,查找慢跳表:是个有序的链表,实现有序数组的二分查找,缺点是占用更多的内存空间。跳表是每隔2个元素选出一......
  • redis5种数据类型使用场景
          ......
  • Redis入门
    参考:小林coding:redisredis官方文档简介redis:用于将各种数据存储在内存中,然后可以将这些存储在内存的数据转存到磁盘中(这就叫持久化)。redis的作用:将数据暂时存储在内存......
  • Redis 利用 incr 和 expire 来限流, 并发导致过期时间失效问题
    当某一个接口需要限流时,可以采用redis的incr来递增,记录访问次数,以及expire来设置失效时间.大概的代码如下:r=redis.Redis.connect()key="linyk3"......
  • redis快速入门
    ​​https://gold.xitu.io/post/5880590d1b69e60058c72803​​​解压后的安装#make指定安装目录:#makePREFIX=/usr/local/redisinstall进入/......
  • 数据库缓存服务——NoSQL之Redis配置与优化
    缓存概念缓存是为了调节速度不一致的两个或多个不同的物质的速度,在中间对速度较慢的一方起到加速作用,比如CPU的一级、二级缓存是保存了CPU最近经常访问的数据,内存是保存C......
  • 非关系型数据库redis部署及优化
    一,关系型数据库与非关系型数据库1.关系型数据库一个结构化的数据库,创建在关系模型(二维表格模型)基础上,一般面向于记录SQL语句(标准数据查询语言)就是一种基于关系型数据......
  • 详解 Redis 中 big keys 发现和解决
    在使用Redis时,可能会出现请求响应慢、网络卡顿、数据丢失的情况。排查问题的时候,发现是bigkeys的问题。什么是bigkeys在Redis中,一个字符串类型最大可以达到512......
  • day23 --> (redis)
    Redis: 1.概念:redis是一款高性能的NOSQL系列的非关系型数据库2.下载安装:1、官网:https://redis.io2、中文网:http://www.redis.net.cn/3.解压可以直接使用:redis.wi......