Redis设置过期时间

【C#代码方式】

一般添加值之后,不设置过期时间的话,ttl某个键会显示-1,表示此时并没有设置过期时间

当设置了过期时间之后,则会显示剩余过期秒数

设置过期时间的做法

                #region 设置过期时间
                //设置多少秒
                client.Set<string>("name", "Adu", TimeSpan.FromSeconds(1));
                Console.WriteLine(client.Get<string>("name"));

                //设置指定日期
                client.Set<string>("Age", "18", DateTime.Now.AddDays(1));
                #endregion

这里的client为声明好的Redis客户端

 

【Redis语句方式】

expire  [keyName]  [seconds]

关键字expire ,后面是键名和过期时间

比如

expire  testKey 50

设置50秒过期

posted @ 2021-05-15 00:56  RookieCoderAdu  阅读(648)  评论(0编辑  收藏  举报