Lesson 4 of 4
Put a Bound on a Stored Answer
Estimated time: 10 minutes
A stored answer needs an age limit
A stored answer saves a call and hides how old it is. With no limit, a value that has changed since it was stored is handed out as if it were current, and the mistake shows up as a wrong result rather than a clear fault.
The limit is a promise, not a guess
The limit says the oldest the answer may be before it is fetched again. Serve inside the limit, fetch outside it, and store the new copy with its own limit. Set it from how fast the answer actually moves, not from how long it would be handy to keep it.
def read(store, key, now, limit):
row = store.get(key)
if row and now - row.stored_at <= limit:
return row.value
fresh = fetch(key)
store[key] = Record(fresh, now)
return fresh
Different answers, different limits
A name changes rarely and a count changes by the minute. One limit for all answers is either too loose for the fast ones or too tight for the slow ones.
Count age from the moment of storing
Measuring age from the last read means an answer that is read often is never allowed to grow old. Count from when it was stored.