sweet

package
v2.0.14 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: Apache-2.0 Imports: 5 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var InvalidCacheKeyErr = errors.New("cache key should be string or implement \"GetCacheKey() string\" method")

Functions

This section is empty.

Types

type Cache added in v2.0.14

type Cache[V any] struct {
	// contains filtered or unexported fields
}

func NewCache added in v2.0.14

func NewCache[V any](back SimpleCache, now func() time.Time) *Cache[V]

func (Cache[V]) Clear added in v2.0.14

func (c Cache[V]) Clear(ctx context.Context)

func (Cache[V]) Get added in v2.0.14

func (c Cache[V]) Get(ctx context.Context, key any) (V, bool)

func (Cache[V]) GetOrProvide added in v2.0.14

func (c Cache[V]) GetOrProvide(ctx context.Context, key any, valueProvider ValueProvider[V]) (V, bool)

func (Cache[V]) GetOrProvideAsync added in v2.0.14

func (c Cache[V]) GetOrProvideAsync(
	ctx context.Context,
	key any,
	valueProvider ValueProvider[V],
	defaultValue V,
) (V, bool)

func (Cache[V]) Remove added in v2.0.14

func (c Cache[V]) Remove(ctx context.Context, key any)

type CacheItem added in v2.0.14

type CacheItem[V any] struct {
	// contains filtered or unexported fields
}

type CacheKeyGenerator added in v2.0.14

type CacheKeyGenerator interface {
	GetCacheKey() string
}

func NewGlobalCacheKeyGenerator added in v2.0.14

func NewGlobalCacheKeyGenerator(namespace string, cacheName string, cacheKeySeparator string, key any) (CacheKeyGenerator, error)

func NewGlobalCacheKeyGeneratorFromPrefix added in v2.0.14

func NewGlobalCacheKeyGeneratorFromPrefix(prefix string, key any) (CacheKeyGenerator, error)

type CacheMonitoring

type CacheMonitoring interface {
	Miss(ctx context.Context, key any)
	Hit(ctx context.Context, key any)
	GetFailed(ctx context.Context, key any, err error)
	PutFailed(ctx context.Context, key any, err error)
	RemoveFailed(ctx context.Context, key any)
	ClearFailed(ctx context.Context)
}

type Cacher

type Cacher[V any] interface {
	GetOrProvide(ctx context.Context, key any, valueProvider ValueProvider[V]) (V, bool)
	GetOrProvideAsync(ctx context.Context, key any, valueProvider ValueProvider[V], defaultValue V) (V, bool)
	Get(ctx context.Context, key any) (V, bool)
	Remove(ctx context.Context, key any)
	Clear(ctx context.Context)
}

type GlobalCacheKeyGenerator added in v2.0.14

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

func (*GlobalCacheKeyGenerator) GetCacheKey added in v2.0.14

func (g *GlobalCacheKeyGenerator) GetCacheKey() string

type GlobalKeysCache added in v2.0.14

type GlobalKeysCache[V any] struct {
	// contains filtered or unexported fields
}

func NewGlobalKeysCache added in v2.0.14

func NewGlobalKeysCache[V any](namespace string, cacheName string, cacheKeySeparator string, back Cacher[V]) *GlobalKeysCache[V]

func (GlobalKeysCache[V]) Clear added in v2.0.14

func (t GlobalKeysCache[V]) Clear(ctx context.Context)

func (GlobalKeysCache[V]) Get added in v2.0.14

func (t GlobalKeysCache[V]) Get(ctx context.Context, key any) (V, bool)

func (GlobalKeysCache[V]) GetOrProvide added in v2.0.14

func (t GlobalKeysCache[V]) GetOrProvide(ctx context.Context, key any, valueProvider ValueProvider[V]) (V, bool)

func (GlobalKeysCache[V]) GetOrProvideAsync added in v2.0.14

func (t GlobalKeysCache[V]) GetOrProvideAsync(ctx context.Context, key any, valueProvider ValueProvider[V], defaultValue V) (V, bool)

func (GlobalKeysCache[V]) Remove added in v2.0.14

func (t GlobalKeysCache[V]) Remove(ctx context.Context, key any)

type NopCacheMonitoring

type NopCacheMonitoring struct {
}

func (NopCacheMonitoring) ClearFailed

func (n NopCacheMonitoring) ClearFailed(ctx context.Context)

func (NopCacheMonitoring) GetFailed

func (n NopCacheMonitoring) GetFailed(ctx context.Context, key any, err error)

func (NopCacheMonitoring) Hit

func (n NopCacheMonitoring) Hit(ctx context.Context, key any)

func (NopCacheMonitoring) Miss

func (n NopCacheMonitoring) Miss(ctx context.Context, key any)

func (NopCacheMonitoring) PutFailed

func (n NopCacheMonitoring) PutFailed(ctx context.Context, key any, err error)

func (NopCacheMonitoring) RemoveFailed

func (n NopCacheMonitoring) RemoveFailed(ctx context.Context, key any)

type SimpleCache added in v2.0.14

type SimpleCache interface {
	// Set adds a key-value pair to the cache.
	// Returns false if key was not set because of some reason
	Set(key any, value any) bool

	// Get retrieves a value from the cache by its key.
	Get(key any) (any, bool)

	// Remove removes a key-value pair from the cache.
	Remove(key any)

	// Clear clears all key-value pairs from the cache.
	Clear()

	// SetWithTTL works like Set but adds a key-value pair to the cache that will expire after the specified TTL
	// (time to live) has passed. A zero value means the value never expires, which is identical to calling Set.
	// A negative value is a no-op and the value is discarded.
	SetWithTTL(key any, value any, ttl time.Duration) bool
}

SimpleCache is an interface for a simple cache that can store key-value pairs.

type SimpleValueProvider added in v2.0.11

type SimpleValueProvider[V any] func(ctx context.Context, key any) (V, error)

type TwoLevelCache added in v2.0.14

type TwoLevelCache[V any] struct {
	// contains filtered or unexported fields
}

func NewTwoLevelCache added in v2.0.14

func NewTwoLevelCache[V any](front Cacher[V], back Cacher[V]) *TwoLevelCache[V]

func (TwoLevelCache[V]) Clear added in v2.0.14

func (t TwoLevelCache[V]) Clear(ctx context.Context)

func (TwoLevelCache[V]) Get added in v2.0.14

func (t TwoLevelCache[V]) Get(ctx context.Context, key any) (V, bool)

func (TwoLevelCache[V]) GetOrProvide added in v2.0.14

func (t TwoLevelCache[V]) GetOrProvide(ctx context.Context, key any, valueProvider ValueProvider[V]) (V, bool)

func (TwoLevelCache[V]) GetOrProvideAsync added in v2.0.14

func (t TwoLevelCache[V]) GetOrProvideAsync(ctx context.Context, key any, valueProvider ValueProvider[V], defaultValue V) (V, bool)

func (TwoLevelCache[V]) Remove added in v2.0.14

func (t TwoLevelCache[V]) Remove(ctx context.Context, key any)

type ValueProvider

type ValueProvider[V any] func(ctx context.Context, key any) (ok bool, val V, actualTTL time.Duration, usableTTL time.Duration)

ValueProvider is a function that provides a value for a given key, along with the actual and usable TTLs.

The function takes a key of type K and returns a value of type V, along with the actual TTL and usable TTL, or an error if the value could not be provided.

Context should be used by provider to properly process cancellation, e.g. because of timeout.

func FixedTTLProvider added in v2.0.11

func FixedTTLProvider[V any](
	defaultActualTTL time.Duration,
	defaultUsableTTL time.Duration,
	defaultActualNegativeTTL time.Duration,
	defaultUsableNegativeTTL time.Duration,
	simpleProvider SimpleValueProvider[V],
) ValueProvider[V]

func SimpleFixedTTLProvider added in v2.0.11

func SimpleFixedTTLProvider[V any](
	defaultUsableTTL time.Duration,
	defaultUsableNegativeTTL time.Duration,
	f func(ctx context.Context, key any) (V, error),
) ValueProvider[V]

func (*ValueProvider[V]) WithRemoteCache added in v2.0.11

func (p *ValueProvider[V]) WithRemoteCache(
	remoteCache Cacher[V],
	baseProvider ValueProvider[V],
) ValueProvider[V]

Jump to

Keyboard shortcuts

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