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-EXECfunctionality:MULTI GET key DELETE key EXECOr 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.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报