We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Cache Reaping

Our cache keeps everything forever. Let's clean up old entries so it doesn't keep growing as we explore.

We'll record when each entry was added, then use a time.Ticker to check for old entries in a background goroutine. This cleanup is called reaping. Use the same interval for the ticker and the maximum entry age. For example, with a 5-second interval, each tick removes entries older than 5 seconds. An entry can stay in the cache until the next tick after it expires.

The cleanup goroutine shares the map with Add and Get. Go maps aren't safe for concurrent reads and writes, so protect all three operations with the same sync.Mutex.

Assignment

Run and submit the CLI tests from the root of the repo.

Tip

A ticker sends on its C channel. You can use for range ticker.C to run the cleanup on each tick. In a test, allow time for another tick after an entry expires; the cleanup won't necessarily run at the exact moment you check.