cachestore

package module
v0.12.3 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2025 License: Apache-2.0 Imports: 6 Imported by: 13

README

cachestore

The cachestore package provides a generic caching interfaces with different backends.

Please see https://github.com/goware/cachestore-examples for example usage.

Cachestore is designed to work with pluggable backends, currently supported:

LICENSE

Copyright (c) 2021-present Sequence Platforms Inc.

Licensed under Apache-2.0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MaxKeyLength = 200

	ErrKeyLengthTooLong = errors.New("cachestore: key length is too long")
	ErrInvalidKey       = errors.New("cachestore: invalid key")
	ErrInvalidKeyPrefix = errors.New("cachestore: invalid key prefix")
	ErrNotSupported     = errors.New("cachestore: not supported")
)
View Source
var ErrBackendAdapterNil = fmt.Errorf("cachestore: backend adapter is nil")
View Source
var ErrBackendTypeCast = fmt.Errorf("cachestore: backend type cast failure")

Functions

func Deserialize

func Deserialize[V any](data []byte) (V, error)

func Serialize

func Serialize[V any](value V) ([]byte, error)

Types

type Backend

type Backend interface {
	Store[any]
}

type ByteStoreGetter

type ByteStoreGetter interface {
	ByteStore() Store[[]byte]
}

type Store

type Store[V any] interface {
	// Name returns the name of the store.
	Name() string

	// Options returns the options for the store.
	Options() StoreOptions

	// Returns true if the key exists.
	Exists(ctx context.Context, key string) (bool, error)

	// Set stores the given value associated to the key.
	Set(ctx context.Context, key string, value V) error

	// SetEx stores the given value associated to the key and sets an expiry ttl
	// for that key.
	SetEx(ctx context.Context, key string, value V, ttl time.Duration) error

	// BatchSet sets all the values associated to the given keys.
	BatchSet(ctx context.Context, keys []string, values []V) error

	// BatchSetEx sets all the values associated to the given keys and sets an
	// expiry ttl for each key.
	BatchSetEx(ctx context.Context, keys []string, values []V, ttl time.Duration) error

	// Get returns a stored value, or nil if the value is not assigned.
	Get(ctx context.Context, key string) (V, bool, error)

	// BatchGet returns the values of all the given keys at once. If any of the
	// keys has no value, nil is returned instead.
	BatchGet(ctx context.Context, keys []string) ([]V, []bool, error)

	// Delete removes a key and its associated value.
	Delete(ctx context.Context, key string) error

	// DeletePrefix removes keys with the given prefix.
	DeletePrefix(ctx context.Context, keyPrefix string) error

	// ClearAll removes all data from the cache. Only use this during debugging,
	// or testing, and never in practice.
	ClearAll(ctx context.Context) error

	// GetOrSetWithLock returns a stored value if it exists. If it doesn't, it acquires
	// a lock and call the getter callback to retrieve a new value. Then it stores that
	// value in the cache and releases the lock.
	GetOrSetWithLock(ctx context.Context, key string, getter func(context.Context, string) (V, error)) (V, error)

	// GetOrSetWithLockEx returns a stored value if it exists. If it doesn't, it acquires
	// a lock and call the getter callback to retrieve a new value. Then it stores that
	// value in the cache, sets expiry ttl for the key and releases the lock.
	GetOrSetWithLockEx(ctx context.Context, key string, getter func(context.Context, string) (V, error), ttl time.Duration) (V, error)
}

func ComposeBackends

func ComposeBackends[V any](backends ...Backend) (Store[V], error)

func ComposeStores

func ComposeStores[V any](stores ...Store[V]) (Store[V], error)

func OpenStore

func OpenStore[T any](backend Backend, opts ...StoreOptions) Store[T]

type StoreOptions

type StoreOptions struct {
	Apply            func(*StoreOptions)
	DefaultKeyExpiry time.Duration

	LockExpiry        time.Duration
	LockRetryTimeout  time.Duration
	LockMinRetryDelay time.Duration
	LockMaxRetryDelay time.Duration
}

func ApplyOptions

func ApplyOptions(opts ...StoreOptions) StoreOptions

func WithDefaultKeyExpiry

func WithDefaultKeyExpiry(keyExpiry time.Duration) StoreOptions

func WithLockExpiry

func WithLockExpiry(lockExpiry time.Duration) StoreOptions

func WithLockRetryDelay

func WithLockRetryDelay(minDelay, maxDelay time.Duration) StoreOptions

func WithLockRetryTimeout

func WithLockRetryTimeout(retryTimeout time.Duration) StoreOptions

type StoreSweeper

type StoreSweeper interface {
	// DeleteExpiredEvery cleans expired keys every d duration.
	// If onError is not nil, it will be called when an error occurs.
	//
	// NOTE: in most cases this is not needed, but for certain stores
	// we need to do this manually.
	DeleteExpiredEvery(ctx context.Context, d time.Duration, onError func(err error))
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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