Skip to main content

Simple as dead support of Redis database for Flask apps.

Project description

Simple as dead support of Redis database for Flask apps.

Requirements

Installation

$ pip install Flask-And-Redis

License

Flask-And-Redis is licensed under the BSD License.

Configuration

As of py-redis 2.4.11 release you should setup next options in your settings module:

  • REDIS_HOST

  • REDIS_PORT

  • REDIS_DB

  • REDIS_PASSWORD

  • REDIS_SOCKET_TIMEOUT

  • REDIS_CONNECTION_POOL

  • REDIS_CHARSET

  • REDIS_ERRORS

  • REDIS_UNIX_SOCKET_PATH

Then all of these args would be sent to redis.Redis.__init__ method.

You also could pass app into initialized instance afterwards with init_app method:

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


app = Flask(__name__)

redis = Redis()
redis.init_app(app)

Advanced

Some times, your redis setting stored as redis://... url (like in Heroku or DotCloud services), sou you could to provide just REDIS_URL value and Flask-And-Redis auto parsed that url and configured then valid redis connection.

Usage

Basic

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


app = Flask(__name__)
redis = Redis(app)

Test application

testapp/app.py

from flask import Flask, redirect, url_for
from flask.ext.redis import Redis

from testapp import settings


# Initialize simple Flask application
app = Flask(__name__)
app.config.from_object(settings)

# Setup Redis conection
redis = Redis(app)

# Add two simple views: One for forgetting counter
@app.route('/forget-us')
def forget_us():
    key = app.config['COUNTER_KEY']
    redis.delete(key)
    return redirect(url_for('home'))


# Second for remembering visiting counter
@app.route('/')
def home():
    key = app.config['COUNTER_KEY']
    counter = redis.incr(key)
    message = 'Hello, visitor!'

    if counter != 1:
        message += "\nThis page viewed %d time(s)." % counter

    return message

testapp/settings.py

COUNTER_KEY = 'testapp:counter'
REDIS_HOST = 'localhost'
REDIS_PORT = 6379
REDIS_DB = 0
# REDIS_URL = 'redis://localhost:6379/0'

Bugs, feature requests?

If you found some bug in Flask-And-Redis library, please, add new issue to the project’s GitHub issues.

Changelog

0.3.2

  • Checked compability with redis-py version 2.6.0.

  • Added redis as install requirement in setup.py.

0.3.1

  • Move from flask_redis package to python module.

  • Little improvements for storing _flask_app attribute to Redis instance.

0.3

  • Implement init_app method.

0.2.1

  • Convert REDIS_PORT to an int instance.

0.2

  • Added support of REDIS_URL setting. By default, Redis will try to guess host, port, user, password and db settings from that value.

0.1

  • Initial release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-And-Redis-0.3.2.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file Flask-And-Redis-0.3.2.tar.gz.

File metadata

File hashes

Hashes for Flask-And-Redis-0.3.2.tar.gz
Algorithm Hash digest
SHA256 5b2038c5de1602c21f58376a7e216203a28e680f6f122c8c0318d01a1e5c94c1
MD5 1503939656809ed91a0341ce53cb632e
BLAKE2b-256 0254ebe7dbd061c6f758210bd89ae0367c16e47c2b71c590fe463aa2100427db

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page