/// <summary> /// 使用CSRedis代替StackChange.Redis /// <remarks> /// 关于CSRedis项目,请参考<seealso cref="https://github.com/2881099/csredis"/> /// </remarks> /// </summary> /// <param name="services"></param> /// <param name="redisConnectionStrings">redis连接字符串。 /// <remarks> /// 如果是单机模式,请只输入一个连接字符串;如果是集群模式,请输入多个连接字符串 /// </remarks> /// </param> /// <returns></returns> public static IServiceCollection UseCsRedisClient(this IServiceCollection services, params string[] redisConnectionStrings) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (redisConnectionStrings == null || redisConnectionStrings.Length == 0) { throw new ArgumentNullException(nameof(redisConnectionStrings)); } CSRedisClient redisClient; if (redisConnectionStrings.Length == 1) { //单机模式 redisClient = new CSRedisClient(redisConnectionStrings[0]); } else { //集群模式 redisClient = new CSRedisClient(null, redisConnectionStrings); } //初始化 RedisHelper RedisHelper.Initialization(redisClient, serialize: value => JsonConvertor.Serialize(value), deserialize: (data, type) => JsonConvertor.Deserialize(data, type)); //注册mvc分布式缓存 services.AddSingleton <IDistributedCache>(new Microsoft.Extensions.Caching.Redis.CSRedisCache(RedisHelper.Instance)); return(services); }
public static T Get <T>(string key) { var value = Get(key); if (!string.IsNullOrEmpty(value)) { return(JsonConvertor.Deserialize <T>(value)); } return(default(T)); }
public static async Task <T> GetAsync <T>(string key) { var value = await GetAsync(key); if (!string.IsNullOrEmpty(value)) { return(JsonConvertor.Deserialize <T>(value)); } return(default(T)); }