cache

package
v0.0.0-...-574c71c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2024 License: Apache-2.0 Imports: 1 Imported by: 4

Documentation

Overview

Package cache implements a cache that supports single-flight value computation.

Single-flight value computation means that for concurrent callers of the cache, only one caller will compute the value to avoid redundant work which could be system intensive.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SingleFlight

type SingleFlight struct {
	// contains filtered or unexported fields
}

SingleFlight is a cache that supports single-flight value computation.

func (*SingleFlight) Delete

func (s *SingleFlight) Delete(key interface{})

Delete removes a key from the cache.

func (*SingleFlight) Load

func (s *SingleFlight) Load(key interface{}) (val interface{}, err error, loaded bool)

Load is similar to a sync.Map.Load.

Callers must check loaded before val and err. Their value is meaninful only if loaded is true. The err is not the last returned value because it would likely lead the reader to think that err must be checked before loaded.

func (*SingleFlight) LoadOrStore

func (s *SingleFlight) LoadOrStore(key interface{}, valFn func() (val interface{}, err error)) (interface{}, error)

LoadOrStore is similar to a sync.Map except that it receives a function that computes the value to store instead of the value directly. It ensures that the function is only executed once for concurrent callers of the LoadOrStore function.

func (*SingleFlight) Reset

func (s *SingleFlight) Reset()

Reset invalidates all cache entries.

func (*SingleFlight) Store

func (s *SingleFlight) Store(key interface{}, val interface{})

Store forcefully updates the given cache key with val. Note that unlike LoadOrStore, Store accepts a value instead of a valFn since it is intended to be only used in cases where updates are lightweight and do not involve computing the cache value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL