flask连接redis

from flask import Flask, render_template, flash, redirect, url_for,session, logging, request
# from data import Articles
# from flask_mysqldb import MySQL
# from wtforms import Form, StringField, TextAreaField, PasswordField, validators
# from passlib.hash import sha256_crypt
# from functools import wraps
import redis
from flask_session import Session
import pickle
import json
import re
from flask_bootstrap import Bootstrap

app = Flask(__name__)
bootstrap = Bootstrap(app)


class Redis:
    @staticmethod
    def connect():
        r = redis.StrictRedis(host='localhost', port=6379)
        return r

    #将内存数据二进制通过序列号转为文本流,再存入redis
    @staticmethod
    def set_data(r,key,data,ex=None):
        r.set(key,pickle.dumps(data),ex)

    # 将文本流从redis中读取并反序列化,返回返回
    @staticmethod
    def get_data(r,key):
        data = r.get(key)
        if data is None:
            return None
        return pickle.loads(data)

@app.route('/', methods=['GET', 'POST'])
# @cache.cached(key_prefix=cache_key)
def login(key):
	# L_phone = []
	cached_redis_remark = ""
	r = Redis.connect()
	Redis.set_data(r, '18333608826', '20190910')
	Redis.set_data(r, '18333608826', '201909')
	times = Redis.get_data(r, '%s' % key)
	# phone = session.hscan_iter('hash1')
	if request.method == 'POST' and times:
		json.dumps(key)
		json.dumps(times)
		return  render_template('index.html', key = key, times = times)
	else:
		msg = "check your phonenumber is correct!"
		# session['username'] = select
		return  render_template('index.html', key = key, msg = msg)


if __name__ == '__main__':
    app.run(debug=False)


# can add debug=True in order to start in development mode

 

你可能感兴趣的:(Python,Flask,Redis)