librelist archives

« back to archive

Cache on Flask-RestLess

Cache on Flask-RestLess

From:
Jociel Andrade
Date:
2013-08-18 @ 14:49
Hello all,

How can I use cache for Flask-RestLess GET methods?

Thanks in advance.

Re: [flask] Cache on Flask-RestLess

From:
Wilberto Morales
Date:
2013-08-18 @ 16:42
I wondered the same once. I ended up implementing a solution that might not
be the best. But I hope it helps. If someone knows a better way it would
help me too.

@application.before_request
def return_cached():
    # if GET and POST not empty
    if not request.values and not request.path.startswith('/static'):
        response = cache.get(request.path)
        if response:
            return response

@application.after_request
def cache_response(response):
    if not request.values and not request.path.startswith('/static'):
        cache.set(request.path, response, timeout=CACHE_TIMEOUT)
    response.headers['Cache-Control'] = 'public, max-age=43200'
    return response


On Sun, Aug 18, 2013 at 9:49 AM, Jociel Andrade <jcainfo@gmail.com> wrote:

> Hello all,
>
> How can I use cache for Flask-RestLess GET methods?
>
> Thanks in advance.
>