

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: State
incomplete
2: PokeAPI
incomplete
3: Cache Add/Get
incomplete
4: Cache Reaping
incomplete
5: Cache Integration
incomplete
6: Explore
incomplete
7: Catch
incomplete
8: Inspect
incomplete
This lesson's interactive features are locked, please to keep using them
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.
Run and submit the CLI tests from the root of the repo.
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.