$npx -y skills add samber/cc-skills-golang --skill golang-samber-hotIn-memory caching in Golang using samber/hot — eviction algorithms (LRU, LFU, TinyLFU, W-TinyLFU, S3FIFO, ARC, TwoQueue, SIEVE, FIFO), TTL, cache loaders, sharding, stale-while-revalidate, missing key caching, and Prometheus metrics. Apply when using or adopting samber/hot, when
| 1 | **Persona:** You are a Go engineer who treats caching as a system design decision. You choose eviction algorithms based on measured access patterns, size caches from working-set data, and always plan for expiration, loader failures, and monitoring. |
| 2 | |
| 3 | # Using samber/hot for In-Memory Caching in Go |
| 4 | |
| 5 | Generic, type-safe in-memory caching library for Go 1.22+ with 9 eviction algorithms, TTL, loader chains with singleflight deduplication, sharding, stale-while-revalidate, and Prometheus metrics. |
| 6 | |
| 7 | **Official Resources:** |
| 8 | |
| 9 | - [pkg.go.dev/github.com/samber/hot](https://pkg.go.dev/github.com/samber/hot) |
| 10 | - [github.com/samber/hot](https://github.com/samber/hot) |
| 11 | |
| 12 | This skill is not exhaustive. Please refer to library documentation and code examples for more information. For Go package docs, symbols, versions, importers, and known vulnerabilities, → See `samber/cc-skills-golang@golang-pkg-go-dev` skill (`godig`) — prefer it over Context7 for Go package facts. To navigate this library's usage in your own code (definitions, call sites, diagnostics), → See `samber/cc-skills-golang@golang-gopls` skill (`gopls`). Context7 remains a fallback for docs not indexed on pkg.go.dev. |
| 13 | |
| 14 | ```bash |
| 15 | go get -u github.com/samber/hot |
| 16 | ``` |
| 17 | |
| 18 | ## Algorithm Selection |
| 19 | |
| 20 | Pick based on your access pattern — the wrong algorithm wastes memory or tanks hit rate. |
| 21 | |
| 22 | | Algorithm | Constant | Best for | Avoid when | |
| 23 | | --- | --- | --- | --- | |
| 24 | | **W-TinyLFU** | `hot.WTinyLFU` | General-purpose, mixed workloads (default) | You need simplicity for debugging | |
| 25 | | **LRU** | `hot.LRU` | Recency-dominated (sessions, recent queries) | Frequency matters (scan pollution evicts hot items) | |
| 26 | | **LFU** | `hot.LFU` | Frequency-dominated (popular products, DNS) | Access patterns shift (stale popular items never evict) | |
| 27 | | **TinyLFU** | `hot.TinyLFU` | Read-heavy with frequency bias | Write-heavy (admission filter overhead) | |
| 28 | | **S3FIFO** | `hot.S3FIFO` | High throughput, scan-resistant | Small caches (<1000 items) | |
| 29 | | **ARC** | `hot.ARC` | Self-tuning, unknown patterns | Memory-constrained (2x tracking overhead) | |
| 30 | | **TwoQueue** | `hot.TwoQueue` | Mixed with hot/cold split | Tuning complexity is unacceptable | |
| 31 | | **SIEVE** | `hot.SIEVE` | Simple scan-resistant LRU alternative | Highly skewed access patterns | |
| 32 | | **FIFO** | `hot.FIFO` | Simple, predictable eviction order | Hit rate matters (no frequency/recency awareness) | |
| 33 | |
| 34 | **Decision shortcut:** Start with `hot.WTinyLFU`. Switch only when profiling shows the miss rate is too high for your SLO. |
| 35 | |
| 36 | For detailed algorithm comparison, benchmarks, and a decision tree, see [Algorithm Guide](./references/algorithm-guide.md). |
| 37 | |
| 38 | ## Core Usage |
| 39 | |
| 40 | ### Basic Cache with TTL |
| 41 | |
| 42 | ```go |
| 43 | import "github.com/samber/hot" |
| 44 | |
| 45 | cache := hot.NewHotCache[string, *User](hot.WTinyLFU, 10_000). |
| 46 | WithTTL(5 * time.Minute). |
| 47 | WithJanitor(). |
| 48 | Build() |
| 49 | defer cache.StopJanitor() |
| 50 | |
| 51 | cache.Set("user:123", user) |
| 52 | cache.SetWithTTL("session:abc", session, 30*time.Minute) |
| 53 | |
| 54 | value, found, err := cache.Get("user:123") |
| 55 | ``` |
| 56 | |
| 57 | ### Loader Pattern (Read-Through) |
| 58 | |
| 59 | Loaders fetch missing keys automatically with singleflight deduplication — concurrent `Get()` calls for the same missing key share one loader invocation: |
| 60 | |
| 61 | ```go |
| 62 | cache := hot.NewHotCache[int, *User](hot.WTinyLFU, 10_000). |
| 63 | WithTTL(5 * time.Minute). |
| 64 | WithLoaders(func(ids []int) (map[int]*User, error) { |
| 65 | return db.GetUsersByIDs(ctx, ids) // batch query |
| 66 | }). |
| 67 | WithJanitor(). |
| 68 | Build() |
| 69 | defer cache.StopJanitor() |
| 70 | |
| 71 | user, found, err := cache.Get(123) // triggers loader on miss |
| 72 | ``` |
| 73 | |
| 74 | ## Capacity Sizing |
| 75 | |
| 76 | Before setting the cache capacity, estimate how many items fit in the memory budget: |
| 77 | |
| 78 | 1. **Estimate single-item size** — estimate size of the struct, add the size of heap-allocated fields (slices, maps, strings). Include the key size. A rough per-entry overhead of ~100 bytes covers internal bookkeeping (pointers, expiry timestamps, algorithm |