cache

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Increment

func Increment[T int | float64](ctx context.Context, cache *RedisCache[T], key string, delta T) (T, error)

Increment is a helper function to increment numeric values

Types

type JSONValueCoder

type JSONValueCoder[T any] struct{}

JSONValueCoder implements ValueCoder using JSON encoding

func (JSONValueCoder[T]) Decode

func (c JSONValueCoder[T]) Decode(encoded string) (T, error)

func (JSONValueCoder[T]) Encode

func (c JSONValueCoder[T]) Encode(value T) (string, error)

type RedisCache

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

RedisCache implements a generic Redis-based cache

func NewCache

func NewCache[T any](client *redis.Client, prefix string) *RedisCache[T]

NewCache creates a new RedisCache instance

func NewStringCache

func NewStringCache(client *redis.Client, prefix string) *RedisCache[string]

NewStringCache creates a new RedisCache instance for string values

func (*RedisCache[T]) Delete

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

Delete removes a value from the cache

func (*RedisCache[T]) DeleteAll

func (c *RedisCache[T]) DeleteAll(ctx context.Context) error

DeleteAll removes all values with the cache's prefix

func (*RedisCache[T]) Exists

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

Exists checks if a key exists in the cache

func (*RedisCache[T]) Get

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

Get retrieves a value from the cache

func (*RedisCache[T]) GetAll

func (c *RedisCache[T]) GetAll(ctx context.Context) (map[string]T, error)

GetAll retrieves all values from the cache

func (*RedisCache[T]) GetWithTTL

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

GetWithTTL retrieves a value and its TTL from the cache

func (*RedisCache[T]) Getdel

func (c *RedisCache[T]) Getdel(ctx context.Context, key string) (T, error)

Getdel retrieves and deletes a value from the cache

func (*RedisCache[T]) Keys

func (c *RedisCache[T]) Keys(ctx context.Context) ([]string, error)

Keys returns all keys in the cache with the given prefix

func (*RedisCache[T]) Set

func (c *RedisCache[T]) Set(ctx context.Context, key string, value T) error

Set stores a value in the cache

func (*RedisCache[T]) SetWithExpiration

func (c *RedisCache[T]) SetWithExpiration(ctx context.Context, key string, value T, expiration time.Duration) error

SetWithExpiration stores a value in the cache with an expiration time

func (*RedisCache[T]) WithCoder

func (c *RedisCache[T]) WithCoder(coder ValueCoder[T]) *RedisCache[T]

WithCoder allows setting a custom value coder

type StringValueCoder

type StringValueCoder struct{}

StringValueCoder implements ValueCoder for string values without JSON encoding

func (StringValueCoder) Decode

func (c StringValueCoder) Decode(encoded string) (string, error)

func (StringValueCoder) Encode

func (c StringValueCoder) Encode(value string) (string, error)

type ValueCoder

type ValueCoder[T any] interface {
	Encode(value T) (string, error)
	Decode(encoded string) (T, error)
}

ValueCoder defines how values are encoded and decoded for Redis storage

Jump to

Keyboard shortcuts

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