← 返回 netflix 的题目列表Design an Auto-Expire Cache
类型:online_judge
Design an Auto-Expire Cache
You need to design a cache system that supports automatic expiration. Your task is to implement the AutoExpireCache class, which includes the following APIs:
__init__(): Initializes the cache object.
set(key, value, timestamp, expire_period): Sets a key-value pair with a specified expiration period expire_period (in seconds) starting from the given timestamp.
get(key): Returns the value associated with the key. If the key is not in the cache or has expired, return None.
Implementation requirements:
Handle cache misses by returning None.
Do not return error messages or codes when expiration occurs.
Support direct key value overwriting.
Use a dictionary or similar data structure for the implementation.
Test Cases:
Initialize the cache, add multiple key-value pairs, and retrieve them to test basic functionality.
Test the expiration logic by setting expiration and attempting to retrieve keys after their expiration period.
Test the logic for overwriting key values.
Constraints:
The number of cache items is n, where n <= 10^4.
The range of expire_period is [1, 10^6].
Note: You should be able to run the code and write basic tests (using Python's assert).