Flask-Redis Integration
Flask-Redis is a Flask extension that provides a convenient way to integrate Redis into your Flask applications. It abstracts away the direct management of Redis connections, allowing you to easily access a Redis client instance configured through your Flask app's configuration. The current version is 0.4.0, with the last release in 2019, indicating a maintenance-only or stable state.
Common errors
-
TypeError: init_app() got an unexpected keyword argument 'strict'
cause Attempting to pass the `strict` parameter to `init_app()` with flask-redis versions 0.3.0 or later.fixThe `strict` parameter should be passed during the `FlaskRedis` instance creation: `redis_client = FlaskRedis(strict=True)` (if not passing `app` initially) or `redis_client = FlaskRedis(app, strict=True)`. -
KeyError: 'redis'
cause Trying to access the Redis client via `app.extensions['redis']` when the extension has not been initialized or the key is incorrect. This can happen if the config prefix was changed, or if running a version older than 0.3.0 (where the key might have been 'REDIS').fixEnsure `FlaskRedis(app)` or `FlaskRedis().init_app(app)` has been called. If using default config, access via `app.extensions['redis']`. If you changed the config prefix, remember it's lowercased: `app.extensions[your_prefix.lower()]`. -
DeprecationWarning: flask_redis.Redis is deprecated, use flask_redis.FlaskRedis instead
cause Using the old class name `Redis` from `flask_redis` module which was renamed.fixChange your import statement from `from flask_redis import Redis` to `from flask_redis import FlaskRedis` and update all instances of `Redis(...)` to `FlaskRedis(...)`.
Warnings
- breaking The `FlaskRedis.init_app` method no longer accepts a `strict` parameter.
- breaking The extension is now registered under the lowercase version of its config prefix (default: 'redis') in `app.extensions`.
- deprecated The class `flask_redis.Redis` was renamed to `flask_redis.FlaskRedis`.
- deprecated Setting `REDIS_DATABASE` (or equivalent) in config is deprecated in favor of including the database number in `REDIS_URL`.
Install
-
pip install flask-redis
Imports
- FlaskRedis
from flask_redis import FlaskRedis
- Redis
from flask_redis import Redis
from flask_redis import FlaskRedis
Quickstart
import os
from flask import Flask
from flask_redis import FlaskRedis
app = Flask(__name__)
# Configure Redis URL from environment variable or default
app.config['REDIS_URL'] = os.environ.get('REDIS_URL', 'redis://localhost:6379/0')
# Initialize Flask-Redis with the app
redis_client = FlaskRedis(app)
@app.route('/')
def index():
try:
redis_client.set('mykey', 'hello from flask-redis!')
value = redis_client.get('mykey')
if value:
return f"Value from Redis: {value.decode()}"
return "No value found in Redis."
except Exception as e:
return f"Error connecting to Redis: {e}"
if __name__ == '__main__':
# Make sure a Redis server is running or set REDIS_URL environment variable
print(f"Redis URL: {app.config['REDIS_URL']}")
app.run(debug=True)