Memory Cache
Memory Cache
Section titled “Memory Cache”go-zero’s collection.Cache provides a thread-safe, size-bounded, TTL-evicting in-process cache backed by LRU eviction.
Basic Usage
Section titled “Basic Usage”import "github.com/zeromicro/go-zero/core/collection"
c := collection.NewCache(time.Minute) // default TTL = 1 minute
// Setc.Set("user:42", user)
// Getval, ok := c.Get("user:42")if ok { return val.(*User), nil}With Custom TTL per Entry
Section titled “With Custom TTL per Entry”c.SetWithExpire("session:abc", session, 30*time.Minute)Take (Read-Through)
Section titled “Take (Read-Through)”val, err := c.Take("user:42", func() (any, error) { return db.QueryUser(42)})Take is safe for concurrent callers — only one goroutine executes the loader function for the same key (singleflight semantics).
Configuration
Section titled “Configuration”c := collection.NewCache( time.Minute, collection.WithLimit(10000), // max 10 000 items)When to Use
Section titled “When to Use”- Hot data that fits in memory and changes infrequently
- Local deduplication of identical concurrent reads
- Complement to Redis cache for L1/L2 layering