Last active
October 18, 2019 18:48
-
-
Save inesp/609db41a4b8757e7d3d51360713b7849 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
def calculate_users_favorite_color(user, ignore_cache=False): | |
now = datetime.now() | |
use_cache = not ignore_cache | |
color, valid_until = None, None | |
if use_cache: | |
color, valid_until = cache.get(f"user_color_{user.id}") | |
# handle the `valid_until` immediatelly | |
if valid_until < now: | |
return color | |
# this if was skipped, because the valid cache value was already returned | |
...logic...logic... | |
color=.... | |
...logic...logic... | |
if use_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