[关闭]
@StrGlee 2016-10-20T18:17:39.000000Z 字数 794 阅读 1106

flask-redis

INSTALL

  1. pip install flask-redis

Or if you must use easy_install:

  1. alias easy_install="pip install $1"
  2. easy_install flask-redis

Configuration

Your configuration should be declared within your Flask config. Set the URL of your database like this:

  1. REDIS_URL = "redis://:password@localhost:6379/0"
  2. # or
  3. REDIS_URL = "unix://[:password]@/path/to/socket.sock?db=0"

To create the redis instance within your application

  1. from flask import Flask
  2. from flask.ext.redis import FlaskRedis
  3. app = Flask(__name__)
  4. redis_store = FlaskRedis(app)

or

  1. from flask import Flask
  2. from flask.ext.redis import FlaskRedis
  3. redis_store = FlaskRedis()
  4. def create_app():
  5. app = Flask(__name__)
  6. redis_store.init_app(app)
  7. return app

USE

  1. from ..configs import redis_store
  2. @app.route('/')
  3. def index():
  4. potato = redis_store.get('potato', 'None')
  5. if potato is None:
  6. potato = 'potato'
  7. redis_store.set('potato',potato)
  8. return potato
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注