class TokenCache:
def __init__(self):
self.cache = {} # Dictionary to store tokens and their associated data
def store_token(self, token, data):
"""Stores a token and its associated data in the cache."""
self.cache[token] = data
def get_token(self, token):
"""Retrieves data associated with a token from the cache."""
return self.cache.get(token)
def remove_token(self, token):
"""Removes a token from the cache."""
if token in self.cache:
del self.cache[token]
def clear_cache(self):
"""Clears all tokens from the cache."""
self.cache = {}
if __name__ == '__main__':
# Example Usage
cache = TokenCache()
# Simulate obtaining a token
token = "example_token"
data = {"user_id": 123, "expiry": "2024-12-31"}
# Store the token and data
cache.store_token(token, data)
print(f"Token stored: {token}")
# Retrieve the token's data
retrieved_data = cache.get_token(token)
print(f"Retrieved data: {retrieved_data}")
# Remove the token
cache.remove_token(token)
print(f"Token removed: {token}")
# Attempt to retrieve the token again (should return None)
retrieved_data = cache.get_token(token)
print(f"Retrieved data after removal: {retrieved_data}")
#Clear the cache
cache.clear_cache()
print("Cache cleared")
Add your comment