cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoHostProvidedErr error = errors.New("no host provided")
View Source
var RedisModeHa = RedisMode("ha")
View Source
var RedisModeStandalone = RedisMode("standalone")

Functions

func DefaultRedisClient

func DefaultRedisClient() *redis.Client

func NewEncodedCache

func NewEncodedCache[T any](cache cache.CacheInterface[string], transcoder BinaryTranscoder[T], ttl time.Duration) cache.CacheInterface[T]

func NewTransparentRedisCache

func NewTransparentRedisCache[T any](redisClient *redis.Client, transcoder BinaryTranscoder[T], ttl time.Duration) cache.CacheInterface[T]

func RedisClient

func RedisClient(config RedisClientConfig) (*redis.Client, error)

Types

type BinaryTranscoder

type BinaryTranscoder[T any] interface {
	Encode(T) ([]byte, error)
	Decode([]byte) (T, error)
}

type Cache

type Cache[T any] struct {
	*cache.Cache[T]
}

func NewInMemoryCache

func NewInMemoryCache[T any](ttl time.Duration, garbageCollectionRate time.Duration) *Cache[T]

func NewRedisCache

func NewRedisCache[T CplnCacheEntry](ttl time.Duration, redisClient *redis.Client) *Cache[T]

type CacheInterface

type CacheInterface[T any] interface {
	cache.CacheInterface[T]
}

type CollectionCache

type CollectionCache[T any] interface {
	// Exists checks if an item exists in cache by name. This is intended to be faster than Get because it does not decode the item.
	Exists(ctx context.Context, name string) (bool, error)

	// Get retrieves an item from cache by name
	Get(ctx context.Context, name string) (*T, error)

	// Set stores an item in cache
	Set(ctx context.Context, item *T) error

	// SetMany stores multiple items in cache
	SetMany(ctx context.Context, items []*T) error

	// Delete removes an item from cache
	Delete(ctx context.Context, name string) error

	// DeleteMany removes multiple items from cache
	DeleteMany(ctx context.Context, names []string) error

	// ListAll retrieves all cached items
	ListAll(ctx context.Context) ([]*T, error)

	// Clear removes all items from cache
	Clear(ctx context.Context) error

	// IsValid checks if the cache index is valid
	IsValid(ctx context.Context) (bool, error)

	// MarkValid marks the cache as valid with TTL
	MarkValid(ctx context.Context, ttl time.Duration) error
}

CollectionCache defines a generic interface for caching collection entries keyed by name.

func NewRedisCollectionCache

func NewRedisCollectionCache[T any](
	config RedisClientConfig,
	batchSize int,
	prefix string,
	nameOf func(*T) (string, error),
) (CollectionCache[T], error)

NewRedisCollectionCache creates a new Redis-backed generic collection cache. prefix is used as the key prefix; indexKey is the marker key used by IsValid/MarkValid.

type CplnCacheEntry

type CplnCacheEntry interface {
	encoding.BinaryUnmarshaler
	encoding.BinaryMarshaler
}

type CplnRedisStore

type CplnRedisStore[T CplnCacheEntry] struct {
	redisStore.RedisStore
	// contains filtered or unexported fields
}

func (*CplnRedisStore[T]) Clear

func (c *CplnRedisStore[T]) Clear(ctx context.Context) error

func (*CplnRedisStore[T]) Delete

func (c *CplnRedisStore[T]) Delete(ctx context.Context, key any) error

func (*CplnRedisStore[T]) Get

func (c *CplnRedisStore[T]) Get(ctx context.Context, key any) (any, error)

func (*CplnRedisStore[T]) GetType

func (c *CplnRedisStore[T]) GetType() string

func (*CplnRedisStore[T]) GetWithTTL

func (c *CplnRedisStore[T]) GetWithTTL(ctx context.Context, key any) (any, time.Duration, error)

func (*CplnRedisStore[T]) Invalidate

func (c *CplnRedisStore[T]) Invalidate(ctx context.Context, options ...store.InvalidateOption) error

func (*CplnRedisStore[T]) Set

func (c *CplnRedisStore[T]) Set(ctx context.Context, key any, value any, options ...store.Option) error

type EncodedCache

type EncodedCache[T any] struct {
	// contains filtered or unexported fields
}

func (*EncodedCache[T]) Clear

func (r *EncodedCache[T]) Clear(ctx context.Context) error

func (*EncodedCache[T]) Delete

func (r *EncodedCache[T]) Delete(ctx context.Context, key any) error

func (*EncodedCache[T]) Get

func (r *EncodedCache[T]) Get(ctx context.Context, key any) (T, error)

func (*EncodedCache[T]) GetType

func (r *EncodedCache[T]) GetType() string

func (*EncodedCache[T]) Invalidate

func (r *EncodedCache[T]) Invalidate(ctx context.Context, options ...store.InvalidateOption) error

func (*EncodedCache[T]) Set

func (r *EncodedCache[T]) Set(ctx context.Context, key any, object T, options ...store.Option) error

type JsonCplnCacheEntry

type JsonCplnCacheEntry[T any] struct {
	// contains filtered or unexported fields
}

func NewJsonCplnCacheEntry

func NewJsonCplnCacheEntry[T any](payload T) *JsonCplnCacheEntry[T]

func (*JsonCplnCacheEntry[T]) MarshalBinary

func (a *JsonCplnCacheEntry[T]) MarshalBinary() (data []byte, err error)

func (*JsonCplnCacheEntry[T]) Payload

func (a *JsonCplnCacheEntry[T]) Payload() T

func (*JsonCplnCacheEntry[T]) UnmarshalBinary

func (a *JsonCplnCacheEntry[T]) UnmarshalBinary(data []byte) error

type JsonTranscoder

type JsonTranscoder[T any] struct {
}

func (JsonTranscoder[T]) Decode

func (j JsonTranscoder[T]) Decode(bytes []byte) (T, error)

func (JsonTranscoder[T]) Encode

func (j JsonTranscoder[T]) Encode(t T) ([]byte, error)

type RedisClientConfig

type RedisClientConfig struct {
	Hosts      []string
	Password   string
	MasterName string
	MaxRetries int
	Mode       RedisMode
}

type RedisCollectionCache

type RedisCollectionCache[T any] struct {
	// contains filtered or unexported fields
}

RedisCollectionCache implements CollectionCache using Redis. It is parameterized over the cached type T and uses a configurable key prefix.

func (*RedisCollectionCache[T]) Clear

func (c *RedisCollectionCache[T]) Clear(ctx context.Context) error

func (*RedisCollectionCache[T]) Delete

func (c *RedisCollectionCache[T]) Delete(ctx context.Context, name string) error

func (*RedisCollectionCache[T]) DeleteMany

func (c *RedisCollectionCache[T]) DeleteMany(ctx context.Context, names []string) error

func (*RedisCollectionCache[T]) Exists

func (c *RedisCollectionCache[T]) Exists(ctx context.Context, name string) (bool, error)

func (*RedisCollectionCache[T]) Get

func (c *RedisCollectionCache[T]) Get(ctx context.Context, name string) (*T, error)

func (*RedisCollectionCache[T]) IsValid

func (c *RedisCollectionCache[T]) IsValid(ctx context.Context) (bool, error)

func (*RedisCollectionCache[T]) ListAll

func (c *RedisCollectionCache[T]) ListAll(ctx context.Context) ([]*T, error)

func (*RedisCollectionCache[T]) MarkValid

func (c *RedisCollectionCache[T]) MarkValid(ctx context.Context, ttl time.Duration) error

func (*RedisCollectionCache[T]) Set

func (c *RedisCollectionCache[T]) Set(ctx context.Context, item *T) error

func (*RedisCollectionCache[T]) SetMany

func (c *RedisCollectionCache[T]) SetMany(ctx context.Context, items []*T) error

type RedisMode

type RedisMode string

Jump to

Keyboard shortcuts

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