Last active
July 9, 2019 15:06
-
-
Save fbidu/dee0ee1490d8f2b2973f591acde14fdd 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
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