///// <summary> ///// 启用Redis TODO ///// 启用Redis TODO ///// </summary> ///// <param name="configuration">配置</param> ///// <param name="useHybridMode">是否启用混合模式</param> //public static void UseRedis(IConfiguration configuration, bool useHybridMode = false) //{ // RedisCacheOptions options = new RedisCacheOptions(); // SetRedisCacheOptions(configuration, options); // // 设置配置 // DependencyConfigurator.RegisterInstance<IOptions<RedisCacheOptions>, RedisCacheOptions>(options); // if (useHybridMode) // DependencyConfigurator.RegisterType<IRedisCacheManager, DefaultRedisCacheManager>(); // else // DependencyConfigurator.RegisterType<ICacheManager, DefaultRedisCacheManager>(); // Console.WriteLine("Redis注入完成。"); //} /// <summary> /// 设置 /// </summary> /// <param name="configuration">配置</param> /// <param name="options">配置对象</param> internal static void SetRedisCacheOptions(IConfiguration configuration, RedisCacheOptions options) { string host = configuration.GetValue <string>("Cache:Redis:Host"); if (string.IsNullOrWhiteSpace(host)) { throw new ConfigException("Cache.Redis.Host"); } string port = configuration.GetValue <string>("Cache:Redis:Port"); if (string.IsNullOrWhiteSpace(port)) { port = "6379"; } string password = configuration.GetValue <string>("Cache:Redis:Password"); string dbStr = configuration.GetValue <string>("Cache:Redis:Db"); int db = 0; if (!string.IsNullOrWhiteSpace(dbStr) && int.TryParse(dbStr, out int result)) { db = result; } options.Host = host; options.Port = port; options.Password = password; options.Db = db; // 设置缓存区域 CacheKeyConverter.SetRegion(configuration); }
/// <summary> /// 解锁实例 /// </summary> /// <param name="key">key</param> /// <param name="value">值</param> private void UnlockInstance(string key, byte[] value) { RedisKey[] keys = { CacheKeyConverter.GetKeyWithRegion(key) }; RedisValue[] values = { value }; GetDatebase().ScriptEvaluate( UnlockScript, keys, values ); }
public T GetOrAdd <T>(string key, Func <T> getData, TimeSpan?expiry = null) { var value = GetDatebase().StringGet(CacheKeyConverter.GetKeyWithRegion(key)); if (value.HasValue) { return(GetValue <T>(value)); } var mutexKey = string.Format(MutexKeyFormat, key); // 防击穿(热点key并发) var uniqueValue = CreateUniqueLockId(); try { if (SetNotExists(mutexKey, uniqueValue, TimeSpan.FromMinutes(1))) { var result = getData(); _loggerLazy.Value.LogInformation($"GetOrAdd, [key:{key}], call getData func"); //防穿透 if (result == null) { Set(key, default(T), TimeSpan.FromSeconds(1)); } else { Set(key, result, expiry); } return(result); } } finally { UnlockInstance(mutexKey, uniqueValue); } Thread.Sleep(50); return(GetOrAdd(key, getData, expiry)); }
/// <summary> /// 设置不存在的值 /// </summary> /// <param name="key">缓存键</param> /// <param name="value">值</param> /// <param name="expiry">过期时间</param> /// <returns></returns> private bool SetNotExists(string key, byte[] value, TimeSpan?expiry = null) { return(GetDatabase().StringSet(CacheKeyConverter.GetKeyWithRegion(key), value, expiry, When.NotExists)); }
/// <summary> /// 删除指定key /// </summary> /// <param name="key">缓存键</param> /// <returns></returns> public bool Delete(string key) { return(GetDatabase().KeyDelete(CacheKeyConverter.GetKeyWithRegion(key))); }
/// <summary> /// 设置数据 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="key">缓存键</param> /// <param name="value">值</param> /// <param name="expiry">过期时间</param> /// <returns></returns> public bool Set <T>(string key, T value, TimeSpan?expiry = null) { return(GetDatabase().StringSet(CacheKeyConverter.GetKeyWithRegion(key), JsonUtil.ToJson(value), expiry)); }
/// <summary> /// 获取key的数据 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="key">缓存键</param> /// <returns></returns> public T Get <T>(string key) { return(GetValue <T>(GetDatabase().StringGet(CacheKeyConverter.GetKeyWithRegion(key)))); }
public bool Set <T>(string key, T value, TimeSpan?expiry = null) { return(GetDatebase().StringSet(CacheKeyConverter.GetKeyWithRegion(key), JsonHelper.SerializeObject(value), expiry)); }