dpp34603 2016-11-18 10:36
浏览 783
已采纳

如何在Golang中设置Redis密钥的到期时间

I am using golang as my backend.I am storing some token values in redis.I m setting the values HSET and getting the values in HGETALL.I would like to know if there is any function to set the expiry for the keys that i m storing in the redis database.i want the token and its data to be deleted after 1hour. I m using Redigo package for redis. Thanks.Appreciate any help.

I use this to set the struct with has token as key    
redisCon.Do("HMSET", redis.Args{}.Add(hashToken).AddFlat(&dataStruct)...)
  • 写回答

1条回答 默认 最新

  • dt1888 2016-11-18 10:47
    关注

    Redis documentation does not support a command like "HMSETEX". "HMSET" modifies the hashkeys and not the root key. TTL is supported at root key level and not at the hash key level. Hence, in your case you must be doing something like this in a separate call:

    redisCon.Do("EXPIRE", key, ttl)
    

    Which client are you using to connect to redis?

    For redigo you can use this - https://github.com/yadvendar/redigo-wrapper In that use call

    func Expire(RConn *redigo.Conn, key string, ttl int)
    

    For goredis - https://godoc.org/gopkg.in/redis.v5#Client.TTL In this use:

    func (c *Client) TTL(key string) *DurationCmd
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式