atomic_miss_cache

package
v0.0.0-...-963fb65 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoSuchEntry is returned when there is no Value for the given key.
	ErrNoSuchEntry = errors.New("No such entry.")

	ErrNoNilValue = errors.New("Illegal value; Value cannot be nil.")
)

Functions

This section is empty.

Types

type AtomicMissCache

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

AtomicMissCache implements a cache which allows the caller to set a value for a given key only if it is unset, while locking the individual entry. This is convenient when obtaining the value for a key on cache miss is expensive and should only be done if absolutely necessary.

func New

func New(backingCache ICache) *AtomicMissCache

New returns a AtomicMissCache instance which uses the given optional backingCache to read and write through. The returned AtomicMissCache is goroutine-safe if the given backingCache is goroutine-safe.

func (*AtomicMissCache) Cleanup

func (c *AtomicMissCache) Cleanup(ctx context.Context, shouldDelete func(context.Context, string, Value) bool) error

Cleanup runs the given function over every cache entry. For a given entry, if the function returns true, the entry is deleted. Also deletes the entry in the backingCache, if it exists. shouldDelete should not call any methods on the AtomicMissCache. The cache does not stay locked throughout the Cleanup() procedure, so shouldDelete should return true only if the entry should be deleted despite being updated concurrently.

func (*AtomicMissCache) Delete

func (c *AtomicMissCache) Delete(ctx context.Context, key string) error

Delete deletes the value for the given key in the cache. Also deletes the value in the backingCache, if it exists.

func (*AtomicMissCache) ForEach

func (c *AtomicMissCache) ForEach(ctx context.Context, fn func(context.Context, string, Value))

ForEach runs the given function over every cache entry. The function should not call any methods on the AtomicMissCache. Only iterates entries in the local cache; does not load cached entries from the backingCache.

func (*AtomicMissCache) Get

func (c *AtomicMissCache) Get(ctx context.Context, key string) (Value, error)

Get returns the stored value for the given key in the cache. If there is no value in the cache, the backingCache is checked, if it exists. If no value is found in the backingCache, ErrNoSuchEntry is returned.

func (*AtomicMissCache) Set

func (c *AtomicMissCache) Set(ctx context.Context, key string, value Value) error

Set sets the value for the given key in the cache. Writes through to the backingCache if it exists. The given Value should not be nil.

func (*AtomicMissCache) SetIfUnset

func (c *AtomicMissCache) SetIfUnset(ctx context.Context, key string, getVal func(ctx context.Context) (Value, error)) (Value, error)

SetIfUnset checks for the existence of a value for the given key, reading through to the backingCache if it exists and the value is not found in the cache. If no value is found, calls getVal to obtain a value to store. If getVal returns no error, the value is stored in the cache and is written through to the backingCache. Returns the existing or new value. getVal should not return nil with no error. getVal is run with the cache entry locked, so it should not attempt to retrieve the same entry, or a deadlock will occur.

type ICache

type ICache interface {
	// Get returns the value for the given key or ErrNoSuchEntry. Get should
	// not return nil without an error.
	Get(context.Context, string) (Value, error)

	// Set sets the value for the given key.
	Set(context.Context, string, Value) error

	// Delete deletes the value for the given key. This is used for cleanup
	// purposes and can be a no-op for backing caches which implement
	// persistent storage.
	Delete(context.Context, string) error
}

ICache is an interface which defines the behaviors of a backing cache.

type Value

type Value interface{}

Value represents a value stored in the cache.

Jump to

Keyboard shortcuts

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