private string GetCache() { if (!RedisDb.Exists(_cacheKey)) { ReloadCache(); } return(RedisDb.Get(_cacheKey)); }
/// <summary> /// 阻塞方式获取缓存 /// </summary> /// <returns></returns> private string ReadCacheByLock() { if (DateTime.Now > _nextReloadTime) { lock (this) { if (DateTime.Now > _nextReloadTime) { ReloadCache(); } } } return(RedisDb.Get(_cacheList[_reloadCache])); }
/// <summary> /// 启用本地缓存 /// </summary> /// <returns></returns> private string GetCache() { try { if (new Random().Next(0, 10) > 6) { throw new Exception(); } return(RedisDb.Get(cacheKey)); } catch (Exception ex) { return(_localCache[1]); } }
/// <summary> /// 部分阻塞获取缓存 /// </summary> /// <returns></returns> private string ReadCacheByLockExpire() { if (DateTime.Now > _nextReloadTime && !_isLock) { _isLock = true; lock (this) { if (DateTime.Now > _nextReloadTime) { ReloadCache(); } } _isLock = false; } return(RedisDb.Get(_cacheList[_reloadCache])); }
/// <summary> /// 启用二级缓存 /// </summary> /// <returns></returns> private string GetCache2() { string cacheKey2 = "LowerCacheKeyBackup"; try { if (new Random().Next(0, 10) > 6) { throw new Exception(); } return(RedisDb.Get(cacheKey)); } catch (Exception ex) { return(RedisDb.Get(cacheKey2)); } }
private string ReadCache() { if (DateTime.Now > _nextReloadTime) { lock (this) { if (!_isReloading) { SwapCache(); Task.Run(() => { ReloadCache(); }); } } } return(RedisDb.Get(_cacheList[_usingCache])); }
/// <summary> /// 禁止访问业务 /// </summary> /// <returns></returns> private string GetCache3() { if (_exceptionCount > 10) { return("业务繁忙,请稍后访问"); } try { if (new Random().Next(0, 10) > 6) { throw new Exception(); } return(RedisDb.Get(cacheKey)); } catch (Exception ex) { _exceptionCount++; return("业务繁忙,请稍后访问"); } }
/// <summary> /// 字符串 /// </summary> private void ShowStr() { RedisDb.Set("StrKey1", new List <int> { 1, 2, 3, 4, 5 }); RedisDb.Set("StrKey2", new { Name = "LGn", Age = 18 }); RedisDb.Set("StrKey3", new[] { new { Name = "LGn", Age = 18 } }); WriteColorLine(RedisDb.Get("StrKey1"), ConsoleColor.Green); WriteColorLine(RedisDb.Get("StrKey2"), ConsoleColor.Green); WriteColorLine(RedisDb.Get("StrKey3"), ConsoleColor.Green); RedisDb.Set("Incr1", 5); RedisDb.Set("Incr2", 5); RedisDb.IncrBy("Incr1"); //异常 //RedisDb.Set("Incr3", "Abc"); //RedisDb.IncrBy("Incr3"); }