Redis中取得所有Key、过期时间配置与获取、Key过期通知。 - Go语言中文社区

Redis中取得所有Key、过期时间配置与获取、Key过期通知。


 string connection = "127.0.0.1:6379,password=xxxxx,connectRetry=5";

        [TestMethod]
        public void TestSet()
        {
            var mutlti = StackExchange.Redis.ConnectionMultiplexer.Connect(connection);
            var db = mutlti.GetDatabase(0);

            db.HashSetAsync("student1", new StackExchange.Redis.HashEntry[] {
                new StackExchange.Redis.HashEntry("name", "song"),
                new StackExchange.Redis.HashEntry("age", 11),
                new StackExchange.Redis.HashEntry("sex", "boy")
            }).GetAwaiter().GetResult();

            db.HashSetAsync("student2", new StackExchange.Redis.HashEntry[] {
                new StackExchange.Redis.HashEntry("name", "wang"),
                new StackExchange.Redis.HashEntry("age", 22),
                new StackExchange.Redis.HashEntry("sex", "girl")
            }).GetAwaiter().GetResult();


            mutlti.Close(true);

        }
        [TestMethod]
        public void TestGetList()
        {
            var mutlti = StackExchange.Redis.ConnectionMultiplexer.Connect(connection);
            var endpoints = mutlti.GetEndPoints();
            List<string> keyList = new List<string>();
            foreach (var ep in endpoints)
            {
                var server = mutlti.GetServer(ep);
                var keys = server.Keys(0, "*");
                foreach (var item in keys)
                {
                    keyList.Add((string)item);
                }
            }
            var db = mutlti.GetDatabase(0);
            mutlti.Close(true);
            throw new Exception(string.Join(",", keyList));
        }
        [TestMethod]
        public void TestGetTime()
        {
            var mutlti = StackExchange.Redis.ConnectionMultiplexer.Connect(connection);
            var db = mutlti.GetDatabase(0);
            var server = mutlti.GetServer(mutlti.GetEndPoints()[0]);
            var timeNow = server.Time().ToUniversalTime();

            var time = db.KeyTimeToLive("student2");
            var expire = time == null ? (DateTime?)null : timeNow.Add(time.Value); //返回UTC时间。
            throw new Exception(expire.Value.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss"));

        }
        [TestMethod]
        public void TestSetTime()
        {
            var mutlti = StackExchange.Redis.ConnectionMultiplexer.Connect(connection);
            var db = mutlti.GetDatabase(0);
            db.KeyExpire("student2", DateTime.Now.AddHours(2));
        }
        [TestMethod]
        public void TestNotifyOnExpire()
        {
            //URL:https://docs.azure.cn/zh-cn/redis-cache/cache-configure
            //填一个"__keyevent@*__:expired*"试试
            //如果不行的话,再改填成:"Kxg"这试试
            var tsk = Task.Factory.StartNew(() =>
              {
                  var mutlti = StackExchange.Redis.ConnectionMultiplexer.Connect(connection);
                  var subscriber = mutlti.GetSubscriber();
                  subscriber.Subscribe("__keyspace@0__:*", (channel, notificationType) =>
                  {
                      Debug.WriteLine(channel + "|" + notificationType);
                  });
                  while (true)
                  {
                      Thread.Sleep(1000);
                  }
              });
            tsk.Wait();
        }

 

转载于:https://www.cnblogs.com/songxingzhu/p/7120439.html

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/a616735104/article/details/101625606
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2023-01-03 12:20:55
  • 阅读 ( 483 )
  • 分类:Redis

0 条评论

请先 登录 后评论

官方社群

GO教程

推荐文章

猜你喜欢