英文:
best way to store a single time use record in redis using golang
问题
我正在使用golang和go-redis包。
我想在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
得分: 3
这是MULTI-EXEC
功能的完美使用案例:
MULTI
GET key
DELETE key
EXEC
或者在Go语言中:
pipe := client.TxPipeline()
get := pipe.Get("key")
pipe.Del("key")
_, err := pipe.Exec()
fmt.Println(get.Val(), err)
这将确保这两个命令在一个事务中执行,因此要么获取并删除键,要么根本不获取。
英文:
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(get.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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论