redisx

package
v1.0.17 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RedisNil = redis.Nil

Functions

This section is empty.

Types

type Cache added in v1.0.16

type Cache interface {
	Get(ctx context.Context, key string, value interface{}) error
	GetCache(ctx context.Context, key string, value interface{}, ttl *time.Duration, loadFunc func() (interface{}, error)) error
	Set(ctx context.Context, key string, cacheItem *Item) error
	Delete(ctx context.Context, key string) error
	Exists(ctx context.Context, key string) bool
	IsErrCacheMiss(err error) bool
	ListKeysByPrefix(ctx context.Context, prefix string) ([]string, error)
	DeleteByPrefix(ctx context.Context, prefix string) error
}

func NewCache added in v1.0.16

func NewCache(client redis.UniversalClient) Cache

type Client added in v1.0.16

type Client interface {
	Publish(ctx context.Context, channel string, message interface{}) error
	Subscribe(ctx context.Context, channel string) *redis.PubSub
	PSubscribe(ctx context.Context, channel string) *redis.PubSub
	Get(ctx context.Context, key string) (string, error)
	Set(ctx context.Context, key, value string, expiration time.Duration) error
	SubscribeMessage(ctx context.Context, options *SubscribeMessageOptions) func()
	PSubscribeMessage(ctx context.Context, channel string, userIdPrefix string, onMessage func(id, message string)) func()
	Client() redis.UniversalClient
	Close() error
}

func NewClient added in v1.0.16

func NewClient(options *RedisOptions) (Client, error)

type DataSource

type DataSource interface {
	WithTTL(ttl time.Duration) DataSourceBuilder

	// Record CRUD (stored as JSON string)
	Create(ctx context.Context, key string, value any) error
	Update(ctx context.Context, key string, value any) error
	Delete(ctx context.Context, key string) error
	Get(ctx context.Context, key string, dest any) error

	// Index operations
	AddToIndex(ctx context.Context, indexKey, id string, score float64) error
	RemoveFromIndex(ctx context.Context, indexKey, id string) error
	GetAllFromIndex(ctx context.Context, indexKey string, start, stop int64) ([]string, error)

	// Atomic helpers (production-safe)
	UpsertWithIndex(ctx context.Context, key, indexKey, id string, score float64, value any, ttl time.Duration) error
	DeleteWithIndex(ctx context.Context, key, indexKey, id string) error

	// Bulk read
	GetAll(ctx context.Context, indexKey string, destSlice any) error

	// Pub/Sub
	Publish(ctx context.Context, channel string, message any) error
	Subscribe(ctx context.Context, channels ...string) *redis.PubSub
}

func NewDataSource

func NewDataSource(rdb *redis.Client) DataSource

type DataSourceBuilder

type DataSourceBuilder interface {
	Create(ctx context.Context, key string, value any) error
	Update(ctx context.Context, key string, value any) error
	AddToIndex(ctx context.Context, indexKey, id string, score float64) error
	UpsertWithIndex(ctx context.Context, key, indexKey, id string, score float64, value any) error
}

type Item added in v1.0.16

type Item struct {
	Value interface{}
	TTL   *time.Duration
}

type RedisOptions added in v1.0.16

type RedisOptions struct {
	ClusterMode bool
	Addr        string
	DB          int
	Username    string
	Password    string
	TraceEnable bool
}

type Repository

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

func NewRepository

func NewRepository[T any](
	ds DataSource,
	name string,
	env string,
	getIDFn func(*T) string,
) *Repository[T]

func (*Repository[T]) Create

func (s *Repository[T]) Create(ctx context.Context, obj *T) error

func (*Repository[T]) CreateList added in v1.0.14

func (s *Repository[T]) CreateList(ctx context.Context, obj *[]T) error

func (*Repository[T]) Delete

func (s *Repository[T]) Delete(ctx context.Context, id string) error

func (*Repository[T]) FindList

func (s *Repository[T]) FindList(ctx context.Context) ([]T, error)

func (*Repository[T]) FindOne

func (s *Repository[T]) FindOne(ctx context.Context, id string) (*T, error)

func (*Repository[T]) Publish added in v1.0.15

func (s *Repository[T]) Publish(ctx context.Context, channel string, message any) error

func (*Repository[T]) Subscribe added in v1.0.15

func (s *Repository[T]) Subscribe(ctx context.Context, channels ...string) *redis.PubSub

func (*Repository[T]) Update

func (s *Repository[T]) Update(ctx context.Context, obj *T) error

func (*Repository[T]) WithTTL

func (s *Repository[T]) WithTTL(ttl time.Duration) *Repository[T]

type SubscribeMessageOptions added in v1.0.16

type SubscribeMessageOptions struct {
	Channel     string
	OnMessage   func(message string)
	ChannelSize int
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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