doujiao1814 2017-06-20 14:38
浏览 445
已采纳

使用golang在Redis中存储一次使用记录的最佳方法

I am using golang and go-redis package I would like to store a key-value pair in redis (e.g one time token). When this token is read, I generate a permanent token. But the one time token should be deleted once I have read the value. This is to avoid fast-replay attack. What is the best way to implement this. I have been thinking of mutex.

  • 写回答

1条回答 默认 最新

  • doutui9606 2017-06-20 17:04
    关注

    This is a perfect use case for the MULTI-EXEC functionality:

    MULTI
    GET key
    DELETE key
    EXEC
    

    Or in go:

    pipe := client.TxPipeline()
    
    get := pipe.Get("key")
    pipe.Del("key")
    
    _, err := pipe.Exec()
    fmt.Println(incr.Val(), err)
    

    This will ensure that both commands execute in a transaction, so the key will either be retrieved and deleted or not retrieved at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!