Adds Redis support to Flask. Built on top of redis-py.
pip install git+https://github.com/imcery/flask-redis#egg=flask-redis
Or if you use pipenv
pipenv install git+https://github.com/imcery/flask-redis#egg=flask-redis
Your configuration should be declared within your Flask config. Set the URL of your database like this:
REDIS_URL = "redis://:password@localhost:6379/0"
# or
REDIS_URL = "unix://[:password]@/path/to/socket.sock?db=0"
To create the redis instance within your application:
from flask import Flask
from flask_redis import Redis
app = Flask(__name__)
redis_store = Redis(app)
or
from flask import Flask
from flask_redis import Redis
redis_store = Redis()
def create_app():
app = Flask(__name__)
redis.init_app(app)
return app
or maybe you want to use mockredis to make your unit tests simpler.
from flask import Flask
from flask_redis import Redis
app = Flask(__name__)
app.config['REDIS_CONNECTION_CLASS'] = 'mockredis.MockRedis'
redis_store = Redis(app)
Flask-Redis
proxies attribute access to an underlying Redis connection. So
treat it as if it were a regular Redis
instance.
from extensions import redis_store
@app.route('/')
def index():
return redis_store.get('prefix:key')
Protip: The redis-py package currently holds the 'redis' namespace, so if you are looking to make use of it, your Redis object shouldn't be named 'redis'.
For detailed instructions regarding the usage of the client, check the redis-py documentation.
Advanced features, such as Lua scripting, pipelines and callbacks are detailed within the redis-py README.