Hi all, First, thanks to Thaddeus for his Flask-Cache extension. It has some great features that will come in handy. A link for those not familiar with it: http://packages.python.org/Flask-Cache/ I'm sure I'm just making this harder than it should be, but here goes. I am able to successfully create the cache object just after creating my Flask app object. I am confused though as to how to access this cache object in my views files. The structure of my app has the views in a separate file, so I need to import the cache object to be able to use it. However, I'm not sure where to import it from, especially since it's built inside a ``create_app`` method in my application's __init__.py. What I'm thinking about is how you can do something like ``from flask import current_app`` to get the app object. Is there a similar thing with the cache object? Should I make it an attribute of Flask's ``g`` object and get at it that way? Alex
Hi Alex, Thanks! You can instantiate the cache object anywhere and then use init_app() when you are setting up your app object. This is what I do for my applications. ###### extensions.py from flaskext.mail import Mail from flaskext.cache import Cache mail = Mail() cache = Cache() ############## Then in your create_app do this: ###### __init__.py from extensions import cache, mail .... def create_app(...): ... cache.init_app(app) ... ############## -- Thadeus On Wed, Jan 26, 2011 at 11:10 AM, Alex Ezell <aezell@gmail.com> wrote: > Hi all, > First, thanks to Thaddeus for his Flask-Cache extension. It has some great > features that will come in handy. > > A link for those not familiar with it: > http://packages.python.org/Flask-Cache/ > > I'm sure I'm just making this harder than it should be, but here goes. > > I am able to successfully create the cache object just after creating my > Flask app object. I am confused though as to how to access this cache object > in my views files. The structure of my app has the views in a separate file, > so I need to import the cache object to be able to use it. However, I'm not > sure where to import it from, especially since it's built inside a > ``create_app`` method in my application's __init__.py. > > What I'm thinking about is how you can do something like ``from flask > import current_app`` to get the app object. Is there a similar thing with > the cache object? Should I make it an attribute of Flask's ``g`` object and > get at it that way? > > Alex >
Hi Thadeus,
Sorry I misspelled your name earlier.
In the example you give, if you needed that cache object in a third file,
you'd just import it from extensions.py and it would have the configuration
given in the create_app, yes?
For instance:
###### views.py
from extensions import cache
....
@views.route('/')
@cache.cached()
def index():
...
##############
Thanks again for the help and the extension.
Alex
On Wed, Jan 26, 2011 at 11:40 AM, Thadeus Burgess <thadeusb@thadeusb.com>wrote:
> Hi Alex,
>
> Thanks!
>
> You can instantiate the cache object anywhere and then use init_app() when
> you are setting up your app object.
>
> This is what I do for my applications.
>
> ###### extensions.py
> from flaskext.mail import Mail
> from flaskext.cache import Cache
>
> mail = Mail()
> cache = Cache()
> ##############
>
> Then in your create_app do this:
>
> ###### __init__.py
> from extensions import cache, mail
>
> ....
> def create_app(...):
> ...
> cache.init_app(app)
> ...
> ##############
>
> --
> Thadeus
>
>
>
>
>
> On Wed, Jan 26, 2011 at 11:10 AM, Alex Ezell <aezell@gmail.com> wrote:
>
>> Hi all,
>> First, thanks to Thaddeus for his Flask-Cache extension. It has some great
>> features that will come in handy.
>>
>> A link for those not familiar with it:
>> http://packages.python.org/Flask-Cache/
>>
>> I'm sure I'm just making this harder than it should be, but here goes.
>>
>> I am able to successfully create the cache object just after creating my
>> Flask app object. I am confused though as to how to access this cache object
>> in my views files. The structure of my app has the views in a separate file,
>> so I need to import the cache object to be able to use it. However, I'm not
>> sure where to import it from, especially since it's built inside a
>> ``create_app`` method in my application's __init__.py.
>>
>> What I'm thinking about is how you can do something like ``from flask
>> import current_app`` to get the app object. Is there a similar thing with
>> the cache object? Should I make it an attribute of Flask's ``g`` object and
>> get at it that way?
>>
>> Alex
>>
>
>
NP. Yes you are correct! -- Thadeus On Wed, Jan 26, 2011 at 12:25 PM, Alex Ezell <aezell@gmail.com> wrote: > Hi Thadeus, > Sorry I misspelled your name earlier. > > In the example you give, if you needed that cache object in a third file, > you'd just import it from extensions.py and it would have the configuration > given in the create_app, yes? > > For instance: > > ###### views.py > from extensions import cache > > .... > @views.route('/') > @cache.cached() > def index(): > ... > ############## > > Thanks again for the help and the extension. > > Alex > > > On Wed, Jan 26, 2011 at 11:40 AM, Thadeus Burgess <thadeusb@thadeusb.com>wrote: > >> Hi Alex, >> >> Thanks! >> >> You can instantiate the cache object anywhere and then use init_app() when >> you are setting up your app object. >> >> This is what I do for my applications. >> >> ###### extensions.py >> from flaskext.mail import Mail >> from flaskext.cache import Cache >> >> mail = Mail() >> cache = Cache() >> ############## >> >> Then in your create_app do this: >> >> ###### __init__.py >> from extensions import cache, mail >> >> .... >> def create_app(...): >> ... >> cache.init_app(app) >> ... >> ############## >> >> -- >> Thadeus >> >> >> >> >> >> On Wed, Jan 26, 2011 at 11:10 AM, Alex Ezell <aezell@gmail.com> wrote: >> >>> Hi all, >>> First, thanks to Thaddeus for his Flask-Cache extension. It has some >>> great features that will come in handy. >>> >>> A link for those not familiar with it: >>> http://packages.python.org/Flask-Cache/ >>> >>> I'm sure I'm just making this harder than it should be, but here goes. >>> >>> I am able to successfully create the cache object just after creating my >>> Flask app object. I am confused though as to how to access this cache object >>> in my views files. The structure of my app has the views in a separate file, >>> so I need to import the cache object to be able to use it. However, I'm not >>> sure where to import it from, especially since it's built inside a >>> ``create_app`` method in my application's __init__.py. >>> >>> What I'm thinking about is how you can do something like ``from flask >>> import current_app`` to get the app object. Is there a similar thing with >>> the cache object? Should I make it an attribute of Flask's ``g`` object and >>> get at it that way? >>> >>> Alex >>> >> >> >