cache

package
v1.0.0-gitspaces-beta Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K any, V any] interface {
	Stats() (int64, int64)
	Get(ctx context.Context, key K) (V, error)
}

Cache is an abstraction of a simple cache.

type Codec

type Codec[V any] interface {
	Encoder[V]
	Decoder[V]
}

type Decoder

type Decoder[V any] interface {
	Decode(encoded string) (V, error)
}

type Encoder

type Encoder[V any] interface {
	Encode(value V) string
}

type ExtendedCache

type ExtendedCache[K comparable, V Identifiable[K]] interface {
	Cache[K, V]
	Map(ctx context.Context, keys []K) (map[K]V, error)
}

ExtendedCache is an extension of the simple cache abstraction that adds mapping functionality.

type ExtendedGetter

type ExtendedGetter[K comparable, V Identifiable[K]] interface {
	Getter[K, V]
	FindMany(ctx context.Context, keys []K) ([]V, error)
}

type ExtendedTTLCache

type ExtendedTTLCache[K constraints.Ordered, V Identifiable[K]] struct {
	TTLCache[K, V]
	// contains filtered or unexported fields
}

ExtendedTTLCache is an extended version of the TTLCache.

func NewExtended

func NewExtended[K constraints.Ordered, V Identifiable[K]](
	getter ExtendedGetter[K, V],
	maxAge time.Duration,
) *ExtendedTTLCache[K, V]

NewExtended creates a new TTLCacheExtended instance and a background routine that periodically purges stale items.

func (*ExtendedTTLCache[K, V]) Map

func (c *ExtendedTTLCache[K, V]) Map(ctx context.Context, keys []K) (map[K]V, error)

Map returns map with all objects requested through the slice of IDs.

type Getter

type Getter[K any, V any] interface {
	Find(ctx context.Context, key K) (V, error)
}

type Identifiable

type Identifiable[K comparable] interface {
	Identifier() K
}

type NoCache

type NoCache[K any, V any] struct {
	// contains filtered or unexported fields
}

func NewNoCache

func NewNoCache[K any, V any](getter Getter[K, V]) NoCache[K, V]

func (NoCache[K, V]) Get

func (c NoCache[K, V]) Get(ctx context.Context, key K) (V, error)

func (NoCache[K, V]) Stats

func (c NoCache[K, V]) Stats() (int64, int64)

type Redis

type Redis[K any, V any] struct {
	// contains filtered or unexported fields
}

func NewRedis

func NewRedis[K any, V any](
	client redis.UniversalClient,
	getter Getter[K, V],
	keyEncoder func(K) string,
	codec Codec[V],
	duration time.Duration,
) *Redis[K, V]

func (*Redis[K, V]) Get

func (c *Redis[K, V]) Get(ctx context.Context, key K) (V, error)

Get implements the cache.Cache interface.

func (*Redis[K, V]) Stats

func (c *Redis[K, V]) Stats() (int64, int64)

Stats returns number of cache hits and misses and can be used to monitor the cache efficiency.

type TTLCache

type TTLCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

TTLCache is a generic TTL based cache that stores objects for the specified period. The TTLCache has no maximum capacity, so the idea is to store objects for short period. The goal of the TTLCache is to reduce database load. Every instance of TTLCache has a background routine that purges stale items.

func New

func New[K comparable, V any](getter Getter[K, V], maxAge time.Duration) *TTLCache[K, V]

New creates a new TTLCache instance and a background routine that periodically purges stale items.

func (*TTLCache[K, V]) Get

func (c *TTLCache[K, V]) Get(ctx context.Context, key K) (V, error)

Get returns one object by its ID.

func (*TTLCache[K, V]) Stats

func (c *TTLCache[K, V]) Stats() (int64, int64)

Stats returns number of cache hits and misses and can be used to monitor the cache efficiency.

func (*TTLCache[K, V]) Stop

func (c *TTLCache[K, V]) Stop()

Stop stops the internal purger of stale elements.

Jump to

Keyboard shortcuts

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