go-samber-hot

Implements in-memory caching in Go using samber/hot with eviction algorithms, TTL, loaders, and Prometheus metrics.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill go-samber-hot-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-samber-hot
Source: https://github.com/asarchami/dotfiles/tree/main/dot_claude/skills/go-samber-hot
Command: npx skills add https://github.com/asarchami/dotfiles --skill go-samber-hot-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires github.com/samber/hot, and includes references (resource) components.

What problem does it solve? Choosing and configuring an in-memory cache in Go involves hard decisions: which eviction algorithm fits your access pattern, how to size capacity against a memory budget, and how to handle TTL, loader failures, and monitoring. This Skill guides those decisions for the samber/hot library so caches are sized correctly and instrumented from the start. ## Core Features & Use Cases - Algorithm Selection: Decision tree and comparison matrix for 9 eviction algorithms (LRU, LFU, TinyLFU, W-TinyLFU, S3FIFO, ARC, TwoQueue, SIEVE, FIFO) matched to access patterns. - Production Patterns: Stale-while-revalidate, sharding for lock contention, missing-key (negative) caching, loader chains with singleflight deduplication, copy-on-read/write, and warm-up on startup. - Capacity Sizing & Monitoring: Memory-budget-based capacity calculation plus Prometheus metrics setup with PromQL queries for hit rate and eviction rate. - Use Case: A service repeatedly queries the same user records from PostgreSQL. Use this Skill to build a W-TinyLFU cache with a batch loader, TTL with jitter, and Prometheus metrics, cutting database load while keeping hit rate above 80%. ## Quick Start Ask the AI to add an in-memory cache using samber/hot with a loader and TTL for the repeated database lookups in your Go service.

Frequently Asked Questions about go-samber-hot

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add an in-memory cache in Go with samber/hot?

Create a cache with hot.NewHotCache[K, V](hot.WTinyLFU, capacity), chain .WithTTL() and .WithJanitor(), then call .Build(). Use Set and Get for access, and defer cache.StopJanitor() to clean up the background expiration goroutine.

Which cache eviction algorithm should I use: LRU, LFU, or W-TinyLFU?

Start with W-TinyLFU, which balances recency and frequency for mixed workloads. Use LRU for recency-dominated patterns like sessions, LFU for stable popularity rankings, and S3FIFO for high-throughput large caches. Switch only when profiling shows miss rates above your SLO.

How do I size cache capacity based on memory budget?

Estimate per-entry size (struct fields plus key plus roughly 100 bytes overhead), then divide your memory budget by that size. For example, 650 bytes per entry in a 256 MB budget yields about 393,000 items. Measure actual size with runtime.ReadMemStats if unknown.

Does samber/hot support stale-while-revalidate caching?

Yes. Use WithRevalidation(staleDuration, loaders) so entries past TTL are served immediately while a background refresh runs. Choose hot.KeepOnError to retain stale values on refresh failure or hot.DropOnError to drop them.

Why does my samber/hot cache panic when calling SetMissing?

SetMissing panics unless a missing-key cache is configured in the builder. Enable it with WithMissingCache(algorithm, capacity) for a dedicated negative cache or WithMissingSharedCache() to store missing entries in the main cache.

How do I monitor samber/hot cache hit rate with Prometheus?

Add WithPrometheusMetrics(cacheName) to the builder and register the cache with prometheus.MustRegister. Query hit ratio by dividing rate(hot_cache_hit_count[5m]) by rate(hot_cache_get_count[5m]); below 80% usually means the cache is undersized or the algorithm is wrong.