From c860f307a88c71117c7de0c444f3670d6ef597e8 Mon Sep 17 00:00:00 2001 From: Swaroop C H Date: Sat, 7 May 2011 13:04:41 +0530 Subject: [PATCH] Adding caching helper --- flask_application/helpers.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/flask_application/helpers.py b/flask_application/helpers.py index 2e5996b..8d6ed7f 100644 --- a/flask_application/helpers.py +++ b/flask_application/helpers.py @@ -2,6 +2,24 @@ import datetime import math +from functools import wraps + +# Caching +def cached(app, timeout=5 * 60, key='view/%s'): + '''http://flask.pocoo.org/docs/patterns/viewdecorators/#caching-decorator''' + def decorator(f): + @wraps(f) + def decorated_function(*args, **kwargs): + cache_key = key % request.path + rv = app.cache.get(cache_key) + if rv is not None: + return rv + rv = f(*args, **kwargs) + app.cache.set(cache_key, rv, timeout=timeout) + return rv + return decorated_function + return decorator + # Custom Template Filters def datetimeformat(value):