librelist archives

« back to archive

Using Flask-Cache and register_module together

Using Flask-Cache and register_module together

From:
Col Wilson
Date:
2010-12-27 @ 23:37
I'm stuck.

I've been building up an app using something like modules approach here:

http://flask.pocoo.org/docs/patterns/packages/#working-with-modules.

However when I try to use:

http://packages.python.org/Flask-Cache/#flaskext.cache.Cache.memoize

I can't work out how I can configure the cache. If I do it here:

from flask import Flask
from flaskext.cache import Cache

app = Flask(__name__)
from application.apis.slots import mod
app.register_module(mod)

cache = Cache(app)

I need to circular import the app, but if I do it in the module:

from flaskext.cache import Cache

mod = Module(__name__)
cache = Cache(mod)

I get "AttributeError: 'Module' object has no attribute 'config' "

Any ideas?

Col

Re: [flask] Using Flask-Cache and register_module together

From:
Thadeus Burgess
Date:
2010-12-28 @ 03:55
Use an application factory as described in

http://flask.pocoo.org/docs/patterns/appfactories/

Create a separate file (such as modules.py) and put

from flaskext.cache import Cache
cache = Cache()

And then from the appfactory

from modules import cache

def init_app():
   ....
   cache.init_app(app)
   ....
   return app

When you need to use memoize on any function, just import the cache object
from the modules.py

--
Thadeus




On Mon, Dec 27, 2010 at 5:37 PM, Col Wilson <colwilson@bcs.org> wrote:

> I'm stuck.
>
> I've been building up an app using something like modules approach here:
>
> http://flask.pocoo.org/docs/patterns/packages/#working-with-modules.
>
> However when I try to use:
>
> http://packages.python.org/Flask-Cache/#flaskext.cache.Cache.memoize
>
> I can't work out how I can configure the cache. If I do it here:
>
> from flask import Flask
> from flaskext.cache import Cache
>
> app = Flask(__name__)
> from application.apis.slots import mod
> app.register_module(mod)
>
> cache = Cache(app)
>
> I need to circular import the app, but if I do it in the module:
>
> from flaskext.cache import Cache
>
> mod = Module(__name__)
> cache = Cache(mod)
>
> I get "AttributeError: 'Module' object has no attribute 'config' "
>
> Any ideas?
>
> Col
>