thadeusb / flask-cache

Cache extension for Flask
http://packages.python.org/Flask-Cache/
Other
697 stars 185 forks source link

Usage inside blueprints #84

Open neoecos opened 10 years ago

neoecos commented 10 years ago

Hi, i would like to get information about the correct way to use Flask-Cache inside flask Blueprints. Thank you,

thadeusb commented 10 years ago

I have not used blueprints extensively. If you ever figured this out would love to add it to the documentation.

fgimian commented 10 years ago

I'll be trying this out myself tomorrow but you should simply be able to import the cache variable and use it in the blueprint.

e.g. let's assume you have this in app.py

from flask.ext.cache import Cache

cache = Cache()

def create_app():
    app = Flask(__name__)
    app.config.from_object('config')
    cache.init_app(app)
    import myblueprint
    app.register_blueprint(myblueprint.mod)
    return app

And then in the blueprint itself in myblueprint.py:

from flask import Blueprint, render_template
from app import cache

mod = Blueprint('myblueprint', __name__)

@mod.route('/')
def index():
    item = cache.get('some_item')
    return render_template('index.html', item=item)

I haven't actually tested this code myself, but it should get you going :smile:

I'll provide further advice tomorrow when I implement this in my project

Cheers Fotis

rturk commented 10 years ago

FYI: Solution provided in http://stackoverflow.com/questions/11020170/using-flask-extensions-in-flask-blueprints