Skip to content

Instantly share code, notes, and snippets.

@scmmishra
Created April 13, 2020 13:20
Show Gist options
  • Save scmmishra/de005a01248d6cda1954a6dc2f3c3a75 to your computer and use it in GitHub Desktop.
Save scmmishra/de005a01248d6cda1954a6dc2f3c3a75 to your computer and use it in GitHub Desktop.
def cache_result(key, for_user=False, expiry=None):
def innerfn(function):
def wrapper(*args, **kwargs):
_cache = cache()
user = None
if for_user:
user = session.user
if kwargs.get("cache"):
value = _cache.get_value(key, user=user)
if value:
return value
result = function(*args, **kwargs)
_cache.set_value(key, result, user=user, expires_in_sec=expiry)
return result
return wrapper
return innerfn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment