flask 连接redis

  • pip3 install flask-redis

from flask import Flask
from flask_redis import FlaskRedis
import time


app = Flask(__name__)
app.config['REDIS_URL'] = 'redis://:[email protected]:6x9/0'
redis_client = FlaskRedis(app)



@app.route('/')
def hello_world():
    return 'Hello World!'


def mark_dyn_data(keys):
    keys_str = str(keys).encode('utf-8')
    exists_keys = redis_client.exists(keys_str)
    print("###exists_keys:", exists_keys)
    if exists_keys == 1:
        redis_client.incrby(keys_str, 1)
    else:
        expires = int(time.time()) + 3600
        p = redis_client.pipeline()
        p.set(keys_str, 1)
        p.expireat(keys_str, expires)
        p.execute()


def get_dyn_data(keys):
    keys_str = str(keys).encode('utf-8')
    data = redis_client.get(keys_str)
    if data:
        return str(data)
    return None

@app.route('/test_redis/')
def test_redis1(keys):
    #mark_dyn_data('icmp_设备无法ping通告警_icmp_10.26.5.36_2022-06-2517:59:38')
    mark_dyn_data(keys)
    data = get_dyn_data(keys)
    return str(data)


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

flask 连接redis_第1张图片

flask 连接redis_第2张图片

你可能感兴趣的:(redisflask)