Practice/Netflix/Auto-Expire Cache
CodingMust
Design and implement a key-value cache with automatic expiration. The system should allow storing key-value pairs that automatically expire after a specified duration.
This problem builds incrementally across multiple parts, with each part extending the previous functionality. It's commonly asked at Netflix as "Log Rate Limiter" and tests your understanding of caching strategies, memory management, and data structure design.
Implement an AutoExpireCache class with basic get/set operations and automatic expiration.
` import time
cache = AutoExpireCache()
cache.set("user_session", {"user_id": 123}, 2)
print(cache.get("user_session")) # {"user_id": 123}
time.sleep(3)
print(cache.get("user_session")) # None
cache.set("feature_flag", False, 60) result = cache.get("feature_flag") print(result) # False print(result is None) # False (it's actually the value False, not None) `
set(key, value, ttl_seconds): Store key-value pair that expires after TTLget(key): Return value if exists and not expired, None otherwiseWhy return None for cache miss instead of False?
Should set return anything?
Error handling for negative TTL or empty keys?