#C13001. TTL Cache Implementation
TTL Cache Implementation
TTL Cache Implementation
Implement a cache system with expiration using Time-To-Live (TTL) values. The cache should support the following operations:
-
SET key value ttl timestamp: Store the key with the given value. The ttl parameter is an integer representing the time-to-live in seconds. If ttl is -1, then the entry never expires. Otherwise, the entry will expire at time = timestamp + ttl.
-
GET key timestamp: Return the value associated with the key if it exists and has not expired at the given timestamp. If the key does not exist or has expired, output 'None'.
-
DEL key timestamp: Delete the key and its associated value from the cache. (The timestamp for DEL is provided for consistency but does not affect the deletion process.)
All operations are given via standard input, and for each GET operation, your program must output the result to standard output. Note that the time provided in each command is a simulated timestamp. Assume all timestamps are non-negative integers and that commands are processed in the order they appear in the input.
inputFormat
The first line of input contains a single integer T, the number of operations. The following T lines each contain an operation in one of the following formats:
-
For setting a key: SET key value ttl timestamp
- key: a string without spaces
- value: a string without spaces (representing an integer value)
- ttl: an integer representing time-to-live in seconds. A value of -1 means the key never expires.
- timestamp: an integer indicating the current time when the key is set.
-
For getting a key: GET key timestamp
- key: the key to retrieve
- timestamp: the current time when the retrieval is attempted.
-
For deleting a key: DEL key timestamp
- key: the key to delete
- timestamp: the current time when the deletion is executed.
Each command is processed in order.
outputFormat
For each GET command, output the value associated with the key if it exists and has not expired at the given timestamp. Otherwise, output 'None' (without quotes). Each result should be printed on a separate line.## sample
5
SET foo 1 5 1000
GET foo 1001
GET foo 1006
DEL foo 1007
GET foo 1008
1
None
None
</p>