A nice way to use Redis in your Flask app
A nice way to use Redis in your Flask app.
Start by installing the extension with pip install flask-redis
.
Once that's done, configure it within your Flask config.
Set the URL of your Redis instance like this:
REDIS_URL = "redis://:password@localhost:6379/0"
If you wanna connect to a Unix socket,
you can specify it like "unix://:password@/path/to/socket.sock?db=0"
.
To add a Redis client to your application:
from flask import Flask
from flask_redis import FlaskRedis
app = Flask(__name__)
redis_client = FlaskRedis(app)
or if you prefer, you can do it the other way around:
redis_client = FlaskRedis(app)
def create_app():
app = Flask(__name__)
redis_client.init_app(app)
return app
The redis client you created above from FlaskRedis
acts just like a regular Redis
instance from the redis-py
library:
from my_app import redis_client
@app.route('/')
def index():
return redis_client.get('potato')
For detailed instructions on what methods you can use on the client, as well as how you can use advanced features such as Lua scripting, pipelines, and callbacks, please check the redis-py documentation.
Pro-tip: The redis-py
package uses the redis
namespace, so it's nicer to name your Redis object something like redis_client
instead of just redis
.
Instead of the default Redis
client from redis-py
,
you can provide your own.
This can be useful to replace it with mockredis for testing:
from flask import Flask
from flask_redis import FlaskRedis
from mockredis import MockRedis
def create_app():
app = Flask(__name__)
if app.testing:
redis_store = FlaskRedis.from_custom_provider(MockRedis)
else:
redis_store = FlaskRedis()
redis_store.init_app(app)
return app
Merging will require a test which shows that the bug was fixed, or that the feature works as expected. Feel free to open a draft pull request though without such a test and ask for help with writing it if you're not sure how to.
As Bence (the only maintainer) works full-time, please allow some time before your issue or pull request is handled.
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog and this project adheres to Semantic Versioning.
FlaskRedis.init_app
method no
longer takes a strict
parameter. Pass this flag when creating your
FlaskRedis
instance, instead.'REDIS'
, so unless you change that, you can still
access the extension via app.extensions['redis']
as before.redis.StrictRedis
. You can switch back to the old redis.Redis
class by specifying strict=False
in the FlaskRedis
kwargs.Redis
keyword arguments (such as
decode_responses
) to FlaskRedis
and they will be correctly
passed over to the redis-py
instance. Thanks, @giyyapan!redis_store['key'] = value
, redis_store['key']
, and
del redis_store['key']
is now supported. Thanks, @ariscn!flask_redis.Redis
to
flask_redis.FlaskRedis
. Using the old name still works, but emits
a deprecation warning, as it will be removed from the next versionREDIS_DATABASE
(or equivalent) now
emits a deprecation warning as it will be removed in the version in
favor of including the database number in REDIS_URL
(or
equivalent)FlaskRedis.from_custom_provider(provider)
class method for
using any redis provider class that supports instantiation with a
from_url
class methodstrict
parameter to FlaskRedis
which expects a boolean
value and allows choosing between using redis.StrictRedis
and
redis.Redis
as the defualt provider.FlaskRedis
register as a Flask extension through Flask's
extension API__getattr__
magic method to pass calls to the underlying
clientThe flask-redis
project is written and maintained
by Bence Nagy (underyx).
The project was originally created by Rhys Elsmore, who maintained it until the 0.0.6 release in 2014. His work was licensed under the Apache 2 license. The project has gone through a full rewrite since, but his work was essential as inspiration. Thanks, Rhys!
A full list of contributors can be found on GitHub's Contributors page
or you can obtain it on your own by running git shortlog -sn
.