Skip to content

Instantly share code, notes, and snippets.

@inesp
Last active October 18, 2019 18:44
Show Gist options
  • Save inesp/7c806216c188a14c5f85b9665f59a3e8 to your computer and use it in GitHub Desktop.
Save inesp/7c806216c188a14c5f85b9665f59a3e8 to your computer and use it in GitHub Desktop.
from datetime import datetime
def calculate_users_favorite_color(user, ignore_cache=False):
now = datetime.now()
color, valid_until = None, None
if not ignore_cache:
color, valid_until = cache.get(f"user_color_{user.id}")
if ignore_cache or (valid_until is not None and valid_until > now):
...logic...logic...
color=....
...logic...logic...
if not ignore_cache:
valid_until = now + datetime.timedelta(days=3, hours=12)
cache.set(f"user_color_{user.id}", (color, valid_until))
return color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment