MCPcopy Create free account
hub / github.com/GoogleChrome/webstatus.dev / Get

Method Get

lib/localcache/cache.go:74–92  ·  view source on GitHub ↗

Get retrieves a value from the cache. If a copier function was provided to NewLocalDataCache, this method returns a deep copy of the value. Otherwise, it returns a direct reference. Returning a copy for reference types is crucial for preventing race conditions. It returns cachetypes.ErrCachedDataNot

(
	_ context.Context,
	key K,
)

Source from the content-addressed store, hash-verified

72 copier Copier[V]
73}
74
75// NewLocalDataCache creates a new LocalDataCache instance.
76// It accepts an optional copier function. If a copier is provided, the Get
77// method will return a deep copy of the cached value. This is critical for
78// ensuring thread safety when caching reference types that might be mutated by consumers.
79// This design is chosen to provide type safety and performance, avoiding the use of reflection
80// for deep copying. If the copier is nil, values will be returned by reference.
81func NewLocalDataCache[K comparable, V any](copier Copier[V]) *LocalDataCache[K, V] {
82 data := make(map[K]V)
83
84 return &LocalDataCache[K, V]{
85 data: data,
86 mu: &sync.RWMutex{},
87 copier: copier,
88 }
89}
90
91// Cache stores a value in the cache.
92// If a copier function was provided to NewLocalDataCache, this method stores a
93// deep copy of the value to ensure thread safety and prevent mutations by the caller
94// from corrupting cached data.
95func (c *LocalDataCache[K, V]) Cache(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected