发送给用户用于注册的验证码保存在哪里

where to save the verification code sent to the user for signing up

我对 Django 有点陌生,这是我第一次实现带有短信验证的注册表单。

我拿到用户手机号,生成一个随机数发给他;我希望生成的代码在 30 分钟后过期,之后我不需要它们,因此将它们保存在数据库中并在过期时间后删除它们似乎不是一个好主意。

我想知道是否有人可以帮助我解决 "what is the best way to implement this?"

的问题

非常感谢您

将它们保存在 Redis 中。 Redis 键可以有一个 TTL(Time-To-Live),有 TTL 的键会在时间段后自动删除。

import redis
r = redis.StrictRedis()

# create pin 
r.set("<phone-number>", <sms-pin>)
r.expire("<phone-number>", 1800) # 1800 seconds = 1/2 hour

# get pin
if r.exists("<phone-number>"):
    pin=r.get("<phone-number>")
    ... validate pin
else:
    ... invalid pin

更多文档位于 http://agiliq.com/blog/2015/03/getting-started-with-redis-py/