Olvasd el a kódot figyelmesen
class CacheAside:
def __init__(self, db):
self.cache = {}
self.db = db
self.log = []
def read(self, key):
if key in self.cache:
self.log.append("hit")
return self.cache[key]
self.log.append("miss")
# Fall back to DB.
value = self.db[key]
self.log.append("db")
self.cache[key] = value
return value
def invalidate(self, key):
self.cache.pop(key, None)
DB = {"product:1": "Widget", "product:2": "Gadget"}
c = CacheAside(DB)
c.read("product:1") # first read — cold cache
c.read("product:1") # second read — should hit
c.read("product:2") # different key — cold
c.invalidate("product:1")
c.read("product:1") # invalidated — back to db
print("\n".join(c.log))
# What does this print? Type your prediction.Mit ír ki a program? Írd ide: