Documentation
¶
Overview ¶
Package cache provides a thread-safe key/value store.
Use this package when you need a concurrent-safe cache. The cache provides atomic get-or-set operations to prevent redundant computation for the same key.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[Key comparable, Value any] struct { // contains filtered or unexported fields }
Cache stores key/value pairs.
func New ¶
func New[Key comparable, Value any]() *Cache[Key, Value]
New creates a new Cache instance. The benefit of using Cache instead of a regular map is that Cache is thread safe.
func (*Cache[Key, Value]) Clear ¶
func (c *Cache[Key, Value]) Clear()
Clear removes all the values in the cache.
func (*Cache[Key, Value]) GetOrSet ¶
func (c *Cache[Key, Value]) GetOrSet(key Key, getOrSetFn GetOrSetFn[Key, Value]) (Value, error)
GetOrSet tries to get the value, and if not present, it calls getOrSetFn to fetch and set the value.
type GetOrSetFn ¶
type GetOrSetFn[Key comparable, Value any] func(Key) (Value, error)
GetOrSetFn is used in the GetOrSet function of the Cache interface. If the value is not present, the function gets called.
Click to show internal directories.
Click to hide internal directories.