golang-samber-hot
samber/cc-skills-golang
Type-safe in-memory caching for Go with 9 eviction algorithms, TTL, loaders, and Prometheus metrics.
What is golang-samber-hot?
samber/hot is a generic caching library for Go 1.22+ that provides multiple eviction strategies (LRU, LFU, TinyLFU, W-TinyLFU, S3FIFO, ARC, TwoQueue, SIEVE, FIFO), automatic expiration, loader chains with singleflight deduplication, and built-in monitoring. Use it when your application repeatedly accesses medium-to-low cardinality resources at high frequency and needs to reduce latency or backend load.
- Choose from 9 eviction algorithms optimized for different access patterns (recency, frequency, scan-resistant, self-tuning)
- Automatic TTL expiration with background janitor cleanup and jitter to prevent thundering herd
- Read-through cache loaders with singleflight deduplication for concurrent miss requests
- Sharding, stale-while-revalidate, and missing-key caching patterns for advanced scenarios
- Prometheus metrics integration for monitoring hit rate, evictions, and loader performance
- Type-safe generic API with copy-on-read/write support for mutable cached values
How to install golang-samber-hot
npx skills add https://github.com/samber/cc-skills-golang --skill golang-samber-hot- Go 1.22 or later
- github.com/samber/hot package installed via `go get -u github.com/samber/hot`
- Understanding of your application's access patterns (recency vs. frequency dominated)
- Memory budget allocated for the cache in your service
How to use golang-samber-hot
- 1.Import the hot package and choose an eviction algorithm (start with hot.WTinyLFU for general-purpose use)
- 2.Estimate single-item size and calculate cache capacity based on your memory budget
- 3.Create a cache instance using the builder pattern with hot.NewHotCache, setting capacity and algorithm
- 4.Chain builder methods: WithTTL() for expiration, WithLoaders() for read-through fetching, WithJanitor() for background cleanup, WithPrometheusMetrics() for monitoring
- 5.Call defer cache.StopJanitor() after building to ensure cleanup on shutdown
- 6.Use cache.Get(), cache.Set(), and cache.SetWithTTL() in your application code; check returned error for loader failures
Use cases
- Cache user profiles or product data fetched from a database to reduce query load
- Session storage with automatic expiration for web applications
- DNS or API response caching to reduce latency and downstream service pressure
- Frequency-dominated workloads like popular item rankings or leaderboards
- High-throughput scenarios requiring scan-resistant eviction to prevent pollution from sequential access patterns
- Go backend engineers building services with repeated data access
- Systems engineers optimizing for latency and reducing database/API load
- Teams adopting samber/hot or already importing github.com/samber/hot
- Developers needing to choose between multiple eviction strategies based on measured workload patterns
golang-samber-hot FAQ
Start with hot.WTinyLFU (default) for general-purpose and mixed workloads. Switch only after profiling shows miss rate is too high for your SLO. Use LRU for recency-dominated access (sessions), LFU for frequency-dominated (popular items), S3FIFO for high throughput with scan resistance, and ARC for unknown/shifting patterns.
Expired entries remain in memory until the eviction algorithm removes them, wasting space. Always chain .WithJanitor() in the builder and call defer cache.StopJanitor() after creation.
Estimate the size of a single cached item (struct + heap fields + ~100 bytes overhead), then divide your memory budget by that size. For example, 256 MB budget with 650 bytes per entry ≈ 393,000 items. If unsure, measure with a unit test using runtime.ReadMemStats.
When multiple goroutines call Get() for the same missing key concurrently, the loader is invoked only once and all callers share the result. This prevents thundering herd and duplicate work on cache misses.
Always set TTL with WithTTL(). Use WithJitter(lambda, upperBound) to spread expirations across time so items don't expire together. Monitor hit rate with Prometheus metrics; below 80% usually means undersizing or wrong algorithm choice.
Full instructions (SKILL.md)
Source of truth, from samber/cc-skills-golang.
name: golang-samber-hot description: "In-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 the codebase imports github.com/samber/hot, or when the project repeatedly loads the same medium-to-low cardinality resources at high frequency and needs to reduce latency or backend pressure." user-invocable: true license: MIT compatibility: Designed for Claude Code or similar AI coding agents, and for projects using Golang. metadata: author: samber version: "1.0.4" openclaw: emoji: "🔥" homepage: https://github.com/samber/cc-skills-golang requires: bins: - go install: [] skill-library-version: "0.13.0" allowed-tools: Read Edit Write Glob Grep Bash(go:) Bash(golangci-lint:) Bash(git:*) Agent WebFetch mcp__context7__resolve-library-id mcp__context7__query-docs AskUserQuestion
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.
Using samber/hot for In-Memory Caching in Go
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.
Official Resources:
This skill is not exhaustive. Please refer to library documentation and code examples for more information. Context7 can help as a discoverability platform. For Go package docs, versions, symbols, and known vulnerabilities, → See samber/cc-skills-golang@golang-pkg-go-dev skill.
go get -u github.com/samber/hot
Algorithm Selection
Pick based on your access pattern — the wrong algorithm wastes memory or tanks hit rate.
| Algorithm | Constant | Best for | Avoid when |
|---|---|---|---|
| W-TinyLFU | hot.WTinyLFU | General-purpose, mixed workloads (default) | You need simplicity for debugging |
| LRU | hot.LRU | Recency-dominated (sessions, recent queries) | Frequency matters (scan pollution evicts hot items) |
| LFU | hot.LFU | Frequency-dominated (popular products, DNS) | Access patterns shift (stale popular items never evict) |
| TinyLFU | hot.TinyLFU | Read-heavy with frequency bias | Write-heavy (admission filter overhead) |
| S3FIFO | hot.S3FIFO | High throughput, scan-resistant | Small caches (<1000 items) |
| ARC | hot.ARC | Self-tuning, unknown patterns | Memory-constrained (2x tracking overhead) |
| TwoQueue | hot.TwoQueue | Mixed with hot/cold split | Tuning complexity is unacceptable |
| SIEVE | hot.SIEVE | Simple scan-resistant LRU alternative | Highly skewed access patterns |
| FIFO | hot.FIFO | Simple, predictable eviction order | Hit rate matters (no frequency/recency awareness) |
Decision shortcut: Start with hot.WTinyLFU. Switch only when profiling shows the miss rate is too high for your SLO.
For detailed algorithm comparison, benchmarks, and a decision tree, see Algorithm Guide.
Core Usage
Basic Cache with TTL
import "github.com/samber/hot"
cache := hot.NewHotCache[string, *User](hot.WTinyLFU, 10_000).
WithTTL(5 * time.Minute).
WithJanitor().
Build()
defer cache.StopJanitor()
cache.Set("user:123", user)
cache.SetWithTTL("session:abc", session, 30*time.Minute)
value, found, err := cache.Get("user:123")
Loader Pattern (Read-Through)
Loaders fetch missing keys automatically with singleflight deduplication — concurrent Get() calls for the same missing key share one loader invocation:
cache := hot.NewHotCache[int, *User](hot.WTinyLFU, 10_000).
WithTTL(5 * time.Minute).
WithLoaders(func(ids []int) (map[int]*User, error) {
return db.GetUsersByIDs(ctx, ids) // batch query
}).
WithJanitor().
Build()
defer cache.StopJanitor()
user, found, err := cache.Get(123) // triggers loader on miss
Capacity Sizing
Before setting the cache capacity, estimate how many items fit in the memory budget:
- 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 metadata).
- Ask the developer how much memory is dedicated to this cache in production (e.g., 256 MB, 1 GB). This depends on the service's total memory and what else shares the process.
- Compute capacity —
capacity = memoryBudget / estimatedItemSize. Round down to leave headroom.
Example: *User struct ~500 bytes + string key ~50 bytes + overhead ~100 bytes = ~650 bytes/entry
256 MB budget → 256_000_000 / 650 ≈ 393,000 items
If the item size is unknown, ask the developer to measure it with a unit test that allocates N items and checks runtime.ReadMemStats. Guessing capacity without measuring leads to OOM or wasted memory.
Common Mistakes
- Forgetting
WithJanitor()— without it, expired entries stay in memory until the algorithm evicts them. Always chain.WithJanitor()in the builder anddefer cache.StopJanitor(). - Calling
SetMissing()without missing cache config — panics at runtime. EnableWithMissingCache(algorithm, capacity)orWithMissingSharedCache()in the builder first. WithoutLocking()+WithJanitor()— mutually exclusive, panics.WithoutLocking()is only safe for single-goroutine access without background cleanup.- Oversized cache — a cache holding everything is a map with overhead. Size to your working set (typically 10-20% of total data). Monitor hit rate to validate.
- Ignoring loader errors —
Get()returns(zero, false, err)on loader failure. Always checkerr, not justfound.
Best Practices
- Always set TTL — unbounded caches serve stale data indefinitely because there is no signal to refresh
- Use
WithJitter(lambda, upperBound)to spread expirations — without jitter, items created together expire together, causing thundering herd on the loader - Monitor with
WithPrometheusMetrics(cacheName)— hit rate below 80% usually means the cache is undersized or the algorithm is wrong for the workload - Use
WithCopyOnRead(fn)/WithCopyOnWrite(fn)for mutable values — without copies, callers mutate cached objects and corrupt shared state
For advanced patterns (revalidation, sharding, missing cache, monitoring setup), see Production Patterns.
For the complete API surface, see API Reference.
If you encounter a bug or unexpected behavior in samber/hot, open an issue at https://github.com/samber/hot/issues.
Cross-References
- → See
samber/cc-skills-golang@golang-performanceskill for general caching strategy and when to use in-memory cache vs Redis vs CDN - → See
samber/cc-skills-golang@golang-observabilityskill for Prometheus metrics integration and monitoring - → See
samber/cc-skills-golang@golang-databaseskill for database query patterns that pair with cache loaders - → See
samber/cc-skills@promql-cliskill for querying Prometheus cache metrics via CLI
Related skills
More from samber/cc-skills-golang and the wider catalog.
golang-code-style
Go code style conventions for clarity, control flow, and readability—line breaking, variable declarations, and when comments help.
golang-error-handling
Idiomatic Go error handling: wrapping, inspection, structured logging, and production-grade error tracking.
golang-performance
Go performance optimization patterns: identify bottlenecks with profiling, then apply the right fix.
golang-design-patterns
Idiomatic Go design patterns: functional options, constructors, error handling, resource lifecycle, graceful shutdown, and resilience.
golang-testing
Production-ready Go tests with table-driven patterns, testify integration, parallel execution, fuzzing, and leak detection.
golang-security
Security best practices and vulnerability prevention for Go code—injection, crypto, secrets, and authentication.