Watch Spring's
@Cacheableactually skip the database. A hit returns instantly, a miss loads and stores, entries expire on TTL, and@CachePut/@CacheEvictrefresh or clear the cache. The cache-aside pattern (Redis / Caffeine style), made visible — with a live "DB calls saved" count.
▶ Live demo · What it shows · Run locally
@Cacheable is one annotation with a lot of invisible behaviour: when does it skip the method? what does @CachePut do differently? why did my cached value go stale — or never expire? This shows every hit, miss, store, and eviction on a live cache, so the mental model (and the cache-aside pattern behind it) clicks.
@Cacheablehit vs miss — firstgetUser(1)is a miss: the DB runs (~800ms) and the result is cached. Call it again → hit: instant, and the method never runs. The "DB calls saved" counter is the whole point of caching in one number.- The cache-aside flow — Caller →
@Cacheableproxy → check cache → (hit) return, or (miss) → Database → store → return. Animated each call. @CachePut— always runs the method and refreshes the entry (watch therevbump), without short-circuiting like@Cacheable.@CacheEvict— removes one entry (next read misses again) or clears all (allEntries = true).- TTL expiry — entries show a countdown bar; after ~7s they expire and the next call reloads them. The classic "why is my cache stale / why does it never update" made obvious.
@Cacheableskips your method entirely on a hit — so side effects in that method won't run when cached.- Self-invocation bypasses the cache — like
@Transactional, calling a@Cacheablemethod from within the same bean skips the proxy, so nothing is cached. @CachePut≠@Cacheable— Put always executes; Cacheable executes only on a miss.
open index.html # macOS
start index.html # Windows
python -m http.server 8000 # or serve → http://localhost:8000A teaching model — the @Cacheable/@CachePut/@CacheEvict semantics and cache-aside flow match Spring Cache (with Redis, Caffeine, or the simple ConcurrentMapCacheManager); data and timings are simulated.
Ideas: @Cacheable(condition=…, unless=…), key generators, cache stampede / thundering-herd, two-level (L1 Caffeine + L2 Redis) caching. PRs welcome.
If this made Spring caching click, a ⭐ helps others find it.
MIT © dev48v