flask-cache icon indicating copy to clipboard operation
flask-cache copied to clipboard

Usage inside blueprints

Open neoecos opened this issue 11 years ago • 3 comments

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

neoecos avatar Mar 10 '14 16:03 neoecos

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

thadeusb avatar Apr 21 '14 13:04 thadeusb

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

fgimian avatar Apr 21 '14 13:04 fgimian

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

rturk avatar May 14 '14 00:05 rturk