Skip to content

Instantly share code, notes, and snippets.

@fbidu
Last active July 9, 2019 15:06
Show Gist options
  • Save fbidu/dee0ee1490d8f2b2973f591acde14fdd to your computer and use it in GitHub Desktop.
Save fbidu/dee0ee1490d8f2b2973f591acde14fdd to your computer and use it in GitHub Desktop.
class CacheState(Enum):
"""
Enum to diferentiate caches hits and misses for boolean values.
It was created to store OAuth2 token validity in cache. In that
scenario, a cache miss prompts an API call to check the token,
while a cache hit with a False value does not.
VALID and INVALID are bool compatible.
>>> bool(CacheState.INVALID)
False
>>> bool(CacheState.VALID)
True
>>> bool(CacheState.MISSING)
Traceback (most recent call last):
...
ValueError: Missing cache has no boolean value.
"""
VALID = 1
INVALID = 0
MISSING = -1
def __bool__(self):
if self.value == -1:
raise ValueError('Missing cache has no boolean value.')
return bool(self.value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment