Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use Flask's Redis extension?

What is the advantage of using the Flask Redis extension in this example...

from flask import Flask, render_template
from flask.ext.redis import Redis


app = Flask(__app__)
app.config['REDIS_HOST'] = 'localhost'
app.config['REDIS_PORT'] = 6379
app.config['REDIS_DB'] = 0


r = Redis(app)

@app.route("/")
def index():
    return render_template("index.html", **r.hgetall("temp.index"))

...over a regular Redis connection instance?

from flask import Flask, render_template

import redis


r = redis.Redis()

@app.route("/")
def index():
    return render_template("index.html", **r.hgetall("temp.index"))
like image 909
tlovely Avatar asked May 04 '15 21:05

tlovely


1 Answers

Current maintainer of the package here, hi!

I think the two (admittedly minor) conveniences that the package provides are:

  1. Integration with Flask's configuration management, so you can organize all your app configuration in the same place. You can add your Redis database URL next to the one for PostgreSQL or whatever you're using, and it'll be picked up automatically when you initialize the FlaskRedis app.

  2. Automatically attaching to your Flask application, so you don't have to keep importing your Redis instance across modules, as it's already always with you, accessible like this: app.extensions['redis']. But if you want to import it anyway, you can also import the module with import flask.ext.redis.

like image 52
Underyx Avatar answered Sep 18 '22 16:09

Underyx