折腾:
期间,需要利用到Flask-redis去保存数据, 读取数据,验证是否过期等等操作
想要搞清楚,如何在Flask中使用Flask-redis实现这些操作
flask-redis usage example
flask-redis 使用
python – Why use Flask’s Redis extension? – Stack Overflow
python – flask使用redis做缓存的话,怎么和数据库同步或者定期更新到数据库? – SegmentFault
python下使用redis构造一个简单的队列(翻译) | 不懂真人
flask-redis api
才注意到:
Flask-redis基于的:
GitHub – andymccurdy/redis-py: Redis Python Client
其实各种操作,都在这里。
此处直接去使用试试:
redis_store.set(phone, smsCodeStr) |
最后用:
from flask import Flask from flask_redis import FlaskRedis app = Flask(__name__, instance_relative_config=True) redis_store = FlaskRedis(app) savedPhoneSmsCode = redis_store.set(phone, smsCodeStr) gLog.debug(“savedPhoneSmsCode=%s”, savedPhoneSmsCode) readbackSmsCode = redis_store.get(phone) gLog.debug(“readbackSmsCode=%s”, readbackSmsCode) |
输出log:
[2016-10-08 15:48:36,032 DEBUG __init__.py:79 post] args={‘phone’: ‘13833334444’, ‘type’: ‘register’} [2016-10-08 15:48:36,033 DEBUG __init__.py:81 post] type=register [2016-10-08 15:48:36,033 DEBUG __init__.py:85 post] phone=13833334444 [2016-10-08 15:48:36,034 DEBUG __init__.py:89 post] randomNumList=(‘0’, ‘1’, ‘8’, ‘8’, ‘8’, ‘5’) [2016-10-08 15:48:36,034 DEBUG __init__.py:91 post] smsCodeStr=018885 [2016-10-08 15:48:36,034 DEBUG __init__.py:98 post] respJsonDict={‘smscode’: ‘018885’, ‘message’: ‘OK’, ‘code’: 200} [2016-10-08 15:48:36,037 DEBUG __init__.py:101 post] savedPhoneSmsCode=True [2016-10-08 15:48:36,038 DEBUG __init__.py:103 post] readbackSmsCode=018885 |
可以去redis-cli中验证:
(RunningFast) ➜ RunningFast redis-cli 127.0.0.1:6379> get 13833334444 “018885” 127.0.0.1:6379> quit |
然后再去研究:
如何设置过期时间
flask-redis set expired time
->是原生的过期的命令
不是我此处要找的flask的redis中的语法和函数
python – when a redis key expires automatically change value – Stack Overflow
Notification of key expiration in redis python – Stack Overflow
Counting Online Users with Redis | Flask (A Python Microframework)
flask redis 设置 过期时间
Redis设置Key的过期时间 – EXPIRE命令 – 数据搬运工
-》还是通过expire命令去设置值的过期时间的。
Redis 下key的过期时间详解 :expire – 多多米开发 – 博客频道 – CSDN.NET
去试试:
redis.expire(key,expiration);
最后用代码:
savedPhoneSmsCode = redis_store.set(phone, smsCodeStr) gLog.debug(“savedPhoneSmsCode=%s”, savedPhoneSmsCode) setExpireSmsCode = redis_store.expire(phone, 60) gLog.debug(“setExpireSmsCode=%s”, setExpireSmsCode) readbackSmsCode = redis_store.get(phone) gLog.debug(“readbackSmsCode=%s”, readbackSmsCode) |
试了试,是可以的
输出是:
[2016-10-08 16:26:26,407 DEBUG __init__.py:79 post] args={‘phone’: ‘13855556666’, ‘type’: ‘register’} [2016-10-08 16:26:26,408 DEBUG __init__.py:81 post] type=register [2016-10-08 16:26:26,408 DEBUG __init__.py:85 post] phone=13855556666 [2016-10-08 16:26:26,408 DEBUG __init__.py:89 post] randomNumList=(‘0’, ‘5’, ‘2’, ‘2’, ‘6’, ‘0’) [2016-10-08 16:26:26,409 DEBUG __init__.py:91 post] smsCodeStr=052260 [2016-10-08 16:26:26,409 DEBUG __init__.py:98 post] respJsonDict={‘smscode’: ‘052260’, ‘message’: ‘OK’, ‘code’: 200} [2016-10-08 16:26:26,412 DEBUG __init__.py:101 post] savedPhoneSmsCode=True [2016-10-08 16:26:26,412 DEBUG __init__.py:103 post] setExpireSmsCode=True [2016-10-08 16:26:26,413 DEBUG __init__.py:105 post] readbackSmsCode=052260 |
然后去命令中中测试,刚开始的确是可以读到值的:
(RunningFast) ➜ RunningFast redis-cli 127.0.0.1:6379> keys * 1) “rq:job:4752e203-751d-493a-8f0e-e70c621c8c91” 2) “rq:job:84aa6b91-fa0c-4fed-bab2-c95672e13472” 3) “13855556666” 4) “13822224444” 5) “rq:job:04148f3b-69ea-4e84-8b46-6b43bd64c5c3” 6) “rq:job:b948bdf3-6046-4a48-82b4-00e61519d7e8” 7) “rq:workers” 8) “rq:queues” 9) “rq:worker:AY140128113754462e2eZ.26813” 10) “13833334444” 11) “rq:scheduler” 12) “myname” 13) “rq:job:7832e6a9-e7fc-4ff5-90b6-fc1cbdd9df09” 14) “rq:job:4b3cac02-8886-4982-bc92-533d700d232c” 15) “rq:queue:failed” 16) “rq:job:3f16ab04-6bb0-49d6-aab4-b9fd024c9608” 127.0.0.1:6379> get ‘13855556666’ “052260” |
过了端时间,超过60秒后,过期后,再去读,就没有改值了:
127.0.0.1:6379> get ‘13855556666’ (nil) 127.0.0.1:6379> keys * 1) “rq:job:4752e203-751d-493a-8f0e-e70c621c8c91” 2) “rq:job:84aa6b91-fa0c-4fed-bab2-c95672e13472” 3) “13822224444” 4) “rq:job:04148f3b-69ea-4e84-8b46-6b43bd64c5c3” 5) “rq:job:b948bdf3-6046-4a48-82b4-00e61519d7e8” 6) “rq:workers” 7) “rq:queues” 8) “rq:worker:AY140128113754462e2eZ.26813” 9) “13833334444” 10) “rq:scheduler” 11) “myname” 12) “rq:job:7832e6a9-e7fc-4ff5-90b6-fc1cbdd9df09” 13) “rq:job:4b3cac02-8886-4982-bc92-533d700d232c” 14) “rq:queue:failed” 15) “rq:job:3f16ab04-6bb0-49d6-aab4-b9fd024c9608” 127.0.0.1:6379> |
-》说明过期的设置,真的是起效果了。
去看看相关的:
【总结】
最终,用:
from flask import Flask from flask_redis import FlaskRedis app = Flask(__name__, instance_relative_config=True) redis_store = FlaskRedis(app) savedPhoneSmsCode = redis_store.set(phone, smsCodeStr) gLog.debug(“savedPhoneSmsCode=%s”, savedPhoneSmsCode) setExpireSmsCode = redis_store.expire(phone, 60) gLog.debug(“setExpireSmsCode=%s”, setExpireSmsCode) readbackSmsCode = redis_store.get(phone) gLog.debug(“readbackSmsCode=%s”, readbackSmsCode) |
实现了,将短信验证码,key是手机号,值是验证码,保存到了redis,且设置了过期时间
在过期之前,是可以读取到对应的值的
过期后,redis中就读取不到改值了-》可以通过redis中是否可以读取到之前保存的验证码来判断是否过期。