Caching in Scala using Cats Ref
summary
Shows a production cache built with Cats Ref and the concrete problems that appear: a naive get then update with Ref.unsafe causes test state bleed and flaky parallel requests where two fibers race and last write wins, TTL checks are hard to test, and cold starts matter. Fixes are specific: inject a controllable nano clock, use Ref.of not Ref.unsafe, perform atomic changes with modify for atomic read-write, apply memoize to dedupe concurrent computes, and run periodic update-based eviction in the background so tests become deterministic.
AI-written summary · about
chapters (12)
- 0:00Introduction to talk
- 1:56Caching principles overview
- 3:24Trade-offs in caching
- 7:23Naive cache implementation
- 12:05Pitfalls in naive approach
- 15:17Testing time-dependent behavior
- 17:46Refactoring with Cats Ref
- 21:47Memoization benefits
- 23:32Eviction policies
- 24:58Why Cats Ref matters
- 26:24Function signatures summary
- 27:29Q&A
