Exemplo n.º 1
0
 private string GetCache()
 {
     if (!RedisDb.Exists(_cacheKey))
     {
         ReloadCache();
     }
     return(RedisDb.Get(_cacheKey));
 }
Exemplo n.º 2
0
 /// <summary>
 /// 阻塞方式获取缓存
 /// </summary>
 /// <returns></returns>
 private string ReadCacheByLock()
 {
     if (DateTime.Now > _nextReloadTime)
     {
         lock (this)
         {
             if (DateTime.Now > _nextReloadTime)
             {
                 ReloadCache();
             }
         }
     }
     return(RedisDb.Get(_cacheList[_reloadCache]));
 }
Exemplo n.º 3
0
 /// <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]);
     }
 }
Exemplo n.º 4
0
 /// <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]));
 }
Exemplo n.º 5
0
        /// <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));
            }
        }
Exemplo n.º 6
0
        private string ReadCache()
        {
            if (DateTime.Now > _nextReloadTime)
            {
                lock (this)
                {
                    if (!_isReloading)
                    {
                        SwapCache();
                        Task.Run(() =>
                        {
                            ReloadCache();
                        });
                    }
                }
            }

            return(RedisDb.Get(_cacheList[_usingCache]));
        }
Exemplo n.º 7
0
        /// <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("业务繁忙,请稍后访问");
            }
        }
Exemplo n.º 8
0
        /// <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");
        }