cache

package
v0.0.0-...-83cf971 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2022 License: LGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCache is a generic, root level error of all cache errors.
	ErrCache = errors.New("cache")

	// ErrMiss is returned when performing operation on key is not in use.
	// This is a not found error narrowed to cache cases only.
	ErrMiss = fmt.Errorf("%w miss", ErrCache)

	// ErrMalformed is returned whenever an operation cannot be
	// completed because value cannot be serialized or deserialized.
	ErrMalformed = fmt.Errorf("malformed %w", ErrCache)

	// ErrConflict is returned when an operation cannot be completed
	// because of otherwise conflicting state.
	ErrConflict = fmt.Errorf("%w conflict", ErrCache)
)

Functions

This section is empty.

Types

type LocalMemCache

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

LocalMemCache is an in-process, local memory cache implementation of a cache service. The usual approach is to use dedicated services like Memcached or Redis, but for a single process, stateful application, using local memory is a valid option as well. LocalMemCache does not provide snapshots support or any other form of state persistence.

func NewLocalMemCache

func NewLocalMemCache(maxMemory uint64) *LocalMemCache

NewLocalMemCache returns local memory cache intance.

If maxMemory if provided, it defines how many bytes can be used before the LRU starts evicting entries. If set to 0, there is no memory limit and entries are never evicted. Counted memory size is only for the stored serialized value, not for the whole memory an item in cache takes.

func (*LocalMemCache) Del

func (c *LocalMemCache) Del(ctx context.Context, key string) error

func (*LocalMemCache) Flush

func (c *LocalMemCache) Flush()

func (*LocalMemCache) Get

func (c *LocalMemCache) Get(ctx context.Context, key string, dest interface{}) error

func (*LocalMemCache) Set

func (c *LocalMemCache) Set(ctx context.Context, key string, value interface{}, exp time.Duration) error

func (*LocalMemCache) SetNx

func (c *LocalMemCache) SetNx(ctx context.Context, key string, value interface{}, exp time.Duration) error

type Serializable

type Serializable interface {
	CacheSerialize() ([]byte, error)
	CacheDeserialize([]byte) error
}

Serializable interface is implemented by any value that should provide a custom way for serialization and deserialization when interacting with cache raw byte storage.

type Store

type Store interface {
	// Get value stored under given key. Returns ErrMiss if key is not
	// used.
	Get(ctx context.Context, key string, dest interface{}) error

	// Set value under given key. If key is already in use, overwrite it's
	// value with given one and set new expiration time.
	Set(ctx context.Context, key string, value interface{}, exp time.Duration) error

	// SetNx set value under given key only if key is not used. It returns
	// ErrConflict if trying to set value for key that is already in use.
	SetNx(ctx context.Context, key string, value interface{}, exp time.Duration) error

	// Del deletes value under given key. It returns ErrCacheMiss if given
	// key is not used.
	Del(ctx context.Context, key string) error
}

Store is implemented by any backend that provides cache functionality.

func StampedeProtect

func StampedeProtect(s Store) Store

StampedeProtect wraps any cache storage with additional layer preventing from stempede.

Jump to

Keyboard shortcuts

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