Design and implement a durable key-value cache that persists data to storage, ensuring that cached data survives process restarts or crashes. Unlike a traditional in-memory cache that loses all data upon restart, a durable cache should be able to restore its state from a persistent storage system.
Implement a durable key-value cache class DurableKVCache with the following methods:
put(key, value): Insert or update an entry in the cache.get(key): Retrieve the value associated with a given key. If the key does not exist in the cache, return -1.remove(key): Remove the entry associated with a given key from the cache.restore(): Restore the cache state from a persistent storage system.The cache should be able to handle a large number of entries and should minimize the time spent on disk I/O operations. The persistent storage system can be simulated using a file or a database.
DurableKVCache cache = new DurableKVCache("cache_storage.txt");cache.put("key1", 1);cache.put("key2", 2);cache.restore(); (Restore the cache from "cache_storage.txt" after a restart)cache.get("key1"); (Returns 1)cache.remove("key2");cache.get("key2"); (Returns -1)put, remove, or restore operations).`python import json from collections import defaultdict
class DurableKVCache: def init(self, storage_file): self.cache = defaultdict(int) self.storage_file = storage_file self.restore()
def put(self, key, value):
self.cache[key] = value
with open(self.storage_file, "a") as f:
json.dump((key, value), f)
f.write("\n")
def get(self, key):
return self.cache.get(key, -1)
def remove(self, key):
if key in self.cache:
del self.cache[key]
with open(self.storage_file, "r+") as f:
lines = f.readlines()
f.seek(0)
for line in lines:
k, v = json.loads(line.strip())
if k != key:
f.write(json.dumps((k, v)) + "\n")
f.truncate()
def restore(self):
try:
with open(self.storage_file, "r") as f:
for line in f:
key, value = json.loads(line.strip())
self.cache[key] = value
except FileNotFoundError:
pass
cache = DurableKVCache("cache_storage.txt") cache.put("key1", 1) cache.put("key2", 2) cache.restore() print(cache.get("key1")) # Output: 1 cache.remove("key2") print(cache.get("key2")) # Output: -1 `
This solution uses a combination of an in-memory hash map and a file-based persistent storage system to implement the durable cache. The put, get, and remove methods provide the basic cache functionality, while the restore method reloads the cache state from the storage file. The solution minimizes disk I/O operations by only writing to the storage file when necessary and using a simple line-based format for storing key-value pairs.