Exemplo n.º 1
0
        /// <summary>
        /// 刷新缓存
        /// </summary>
        private void ReloadCache()
        {
            _isReloading = true;
            WriteColorLine($"正在执行缓存刷新", ConsoleColor.Green);
            Thread.Sleep(2 * 1000);

            RedisDb.Set(_cacheList[_reloadCache], $"[{DateTime.Now}]{_cacheList[_reloadCache]}");
            RedisDb.Expire(_cacheList[_reloadCache], 30);

            _nextReloadTime = DateTime.Now.AddSeconds(1);
            WriteColorLine($"执行缓存刷新完毕", ConsoleColor.Green);
            _isReloading = false;
        }
Exemplo n.º 2
0
 private void SetCache3(Person model)
 {
     lock (_lock3)
     {
         Task.Run(() =>
         {
             Thread.Sleep(1 * 1000);
             //全表重建缓存
             var cacheKey = $"BrokeBigKey:{SimpleHash(model.Id)}";
             RedisDb.HSet(cacheKey, model.Id.ToString(), model);
             RedisDb.Expire(cacheKey, 1 + new Random().Next(0, 20) / 10);
         });
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// 单个缓存
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private Person GetCache(int id)
        {
            var cacheKey = "PenetrationCache2";
            var getCache = RedisDb.HGet(cacheKey, id.ToString());

            if (getCache == null)
            {
                var dt = GetDbData(id);
                RedisDb.HSet(cacheKey, id.ToString(), dt);
                return(dt);
            }
            RedisDb.Expire(cacheKey, 10);
            return(Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(getCache, new Person()));
        }
Exemplo n.º 4
0
        private void SetCache2(Person model)
        {
            lock (_lock2)
            {
                var allData = GetDbAll();
                Task.Run(() =>
                {
                    var cacheKey = "BrokeBigKey";
                    Thread.Sleep(1 * 1000);
                    //全表重建缓存

                    RedisDb.HSet(cacheKey, model.Id.ToString(), model);
                    RedisDb.Expire(cacheKey, 1);
                });
            }
        }
Exemplo n.º 5
0
        private void DoSomething2(int id)
        {
            var cacheKey = $"RedisLock2:{id}";



            if (RedisDb.SetNx(cacheKey, "1"))
            {
                //关注点1:设置过期时间失败
                RedisDb.Expire(cacheKey, 5);
                WriteColorLine($"正在处理ID【{id}】", ConsoleColor.Green);
                Thread.Sleep(1 * 1000);
                WriteColorLine($"处理ID结束【{id}】", ConsoleColor.Red);

                //关注点2:超时删除
                RedisDb.Del(cacheKey);
            }
            else
            {
                WriteColorLine($"ID【{id}】正在处理中", ConsoleColor.Yellow);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 集合
        /// </summary>
        private void ShowSet()
        {
            //获取人员访问记录
            var userList = new List <string>();

            for (int i = 0; i < 100; i++)
            {
                userList.Add($"Account{i}");
            }
            var cacheKey = "AccountIn";

            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(1);
                RedisDb.SAdd(cacheKey, userList[new Random().Next(0, userList.Count)]);
            }
            RedisDb.Expire(cacheKey, 2);
            WriteColorLine("访问人数:" + RedisDb.SCard(cacheKey), ConsoleColor.Blue);
            foreach (var item in RedisDb.SMembers(cacheKey))
            {
                WriteColorLine(item, ConsoleColor.Blue);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 全表重建缓存导致无法缓存不存在的Key
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private Person GetCache2(int id)
        {
            var cacheKey = "PenetrationCache2";
            var getCache = RedisDb.HGet(cacheKey, id.ToString());

            if (getCache == null)
            {
                //全表重建缓存
                var pinple = RedisDb.StartPipe();
                foreach (var item in GetDbAll())
                {
                    pinple.HSet(cacheKey, item.Id.ToString(), item);
                }
                pinple.EndPipe();
                getCache = RedisDb.HGet(cacheKey, id.ToString());
            }
            RedisDb.Expire(cacheKey, 10);
            if (getCache == null)
            {
                return(GetDbData(id));
            }
            return(Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(getCache, new Person()));
        }
Exemplo n.º 8
0
        /// <summary>
        /// 地图
        /// </summary>
        private void ShowGeo()
        {
            var ran = new Random();

            for (int i = 0; i < 100; i++)
            {
                RedisDb.GeoAdd("GeoKey1", new Random(Guid.NewGuid().GetHashCode()).Next(10400, 10600) / 100m, new Random(Guid.NewGuid().GetHashCode()).Next(7900, 8100) / 100m, $"mem{i}");
            }

            var myPlace = (105, 80);

            RedisDb.GeoAdd("GeoKey1", myPlace.Item1, myPlace.Item2, $"myplace");

            RedisDb.Expire("GeoKey1", 5);

            var getGeo = RedisDb.GeoRadiusByMemberWithDistAndCoord("GeoKey1", "myplace", 10 * 1000);



            foreach (var item in getGeo)
            {
                WriteColorLine($"Name:{item.member},经度:{item.longitude},纬度:{item.latitude},距离:{item.dist}", ConsoleColor.White);
            }
        }