The example's shallow copy does not isolate slices, maps, or nested pointers
Source references: 2The documentation presents the pattern for pointers, slices, and maps and says mutations will not affect the cache, but the example only performs copy := *u. That copies the top-level struct while slices, maps, and nested pointers still share their underlying data.
A caller modifying a nested mutable field can still change the shared cached object, causing cross-request data corruption or races—the problem this configuration claims to prevent.
The guide presents this pattern as required for mutable pointers, slices, and maps and says caller mutations will not affect the cache, but `copy := *u` is only a shallow copy. If User contains slices, maps, or nested pointers, their backing data remains shared, so concurrent mutation can still race or corrupt cached state. It is adequate only when relevant fields are value-only. Users can ask for an explicit shallow-copy warning, a deep-copy example, and race tests using types with reference fields.
## Copy-on-Read / Copy-on-WriteRequired when cached values are mutable (pointers, slices, maps):```gocache := hot.NewHotCache[string, *User](hot.WTinyLFU, 10_000). WithTTL(5 * time.Minute). WithCopyOnRead(func(u *User) *User { copy := *u return © }). WithCopyOnWrite(func(u *User) *User { copy := *u return © }). WithJanitor(). Build()defer cache.StopJanitor()```Show 1 other places
- **CopyOnRead** — clones at retrieval: callers get independent copies, mutations don't affect cache- **CopyOnWrite** — clones at storage: cache holds a snapshot, external mutations to the original don't corrupt cached value- Use both when callers read and write concurrently. Use only one when the mutation direction is known.