v2

package
v0.0.0-...-7a133f2 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	JSONSer    = JSONSerializer{}
	MsgpackSer = MsgPackSerializer{}
)

Functions

func Keys

func Keys[T any](t []T, keyGen func(T) string) []string

func KeysAndMap

func KeysAndMap[T any](t []T, keyGen func(T) string) ([]string, map[string]T)

func RangeKeys

func RangeKeys[T any](keys []string, m map[string]T) []T

func RangeRevertKeys

func RangeRevertKeys[T any, R any](keys []string, m map[string]T, revert func(T) R) []R

遍历keys 从m中找到遍历的key 并回调revert中

Types

type Cache

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

func New

func New[T any](r *redis.Redis) *Cache[T]

func (*Cache[T]) Del

func (c *Cache[T]) Del(ctx context.Context, keys ...string) error

func (*Cache[T]) Get

func (c *Cache[T]) Get(ctx context.Context, key string, opts ...Option) (t T, err error)

func (*Cache[T]) GetOrFetch

func (c *Cache[T]) GetOrFetch(ctx context.Context, key string, fetcher Fetcher[T], opts ...Option) (t T, err error)

func (*Cache[T]) HGetAllOrFetch

func (c *Cache[T]) HGetAllOrFetch(ctx context.Context,
	key string,
	fetcher Fetcher[T],
	opt ...Option) (t T, err error)

you need `redis:"field"` tag in T to make this method work. T must be a struct

func (*Cache[T]) LRange

func (c *Cache[T]) LRange(ctx context.Context, key string, start, stop int, opts ...Option) ([]T, error)

func (*Cache[T]) MGetOrFetch

func (c *Cache[T]) MGetOrFetch(ctx context.Context,
	keys []string,
	fetcher MFetcher[T],
	opts ...Option) (t map[string]T, err error)

func (*Cache[T]) Set

func (c *Cache[T]) Set(ctx context.Context, key string, val T, opts ...Option) error

func (*Cache[T]) Setex

func (c *Cache[T]) Setex(ctx context.Context, key string, val T, seconds int, opts ...Option) error

func (*Cache[T]) SmembersOrFetch

func (c *Cache[T]) SmembersOrFetch(ctx context.Context,
	key string,
	fetcher SortedSetFetcher[T],
	opts ...Option) ([]T, error)

type Fetcher

type Fetcher[T any] func(ctx context.Context) (T, time.Duration, error)

type JSONSerializer

type JSONSerializer struct{}

func (JSONSerializer) Marshal

func (j JSONSerializer) Marshal(v any) ([]byte, error)

func (JSONSerializer) Unmarshal

func (j JSONSerializer) Unmarshal(data []byte, v any) error

type MFetcher

type MFetcher[T any] func(ctx context.Context, keys []string) (map[string]T, error)

type MsgPackSerializer

type MsgPackSerializer struct{}

func (MsgPackSerializer) Marshal

func (j MsgPackSerializer) Marshal(v any) ([]byte, error)

func (MsgPackSerializer) Unmarshal

func (j MsgPackSerializer) Unmarshal(data []byte, v any) error

type Option

type Option func(*cacheOption)

func WithBgSet

func WithBgSet(b bool) Option

func WithSerializer

func WithSerializer(ser Serializer) Option

func WithTTL

func WithTTL(dur time.Duration) Option

type Serializer

type Serializer interface {
	Marshal(any) ([]byte, error)
	Unmarshal([]byte, any) error
}

type SortedSetFetcher

type SortedSetFetcher[T any] func(ctx context.Context) ([]T, error)

Jump to

Keyboard shortcuts

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