redistimer

package
v0.0.50-alpha.103 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New[T any](ctx context.Context, redisConfig *redisutil.Config, keyFunc timer.KeyFunc[T], handlers timer.HandlerMap[T], opts ...Option[T]) (timer.Manager[T], error)

New creates a new Redis-based timer manager and starts processing

func NewWithClient

func NewWithClient[T any](ctx context.Context, client redis.UniversalClient, keyFunc timer.KeyFunc[T], handlers timer.HandlerMap[T], opts ...Option[T]) (timer.Manager[T], error)

NewWithClient creates a timer manager with existing Redis client and starts processing

Types

type Option

type Option[T any] func(*RedisTimer[T])

Option configures a RedisTimer

func WithBatchSize

func WithBatchSize[T any](size int) Option[T]

WithBatchSize sets the batch size for processing expired timers

func WithMarshal

func WithMarshal[T any](fn func(T) ([]byte, error)) Option[T]

WithMarshal sets custom marshal function

func WithNamespace

func WithNamespace[T any](namespace string) Option[T]

WithNamespace sets the Redis key namespace

func WithPollInterval

func WithPollInterval[T any](interval time.Duration) Option[T]

WithPollInterval sets the polling interval for checking expired timers

func WithSelfContainedKey

func WithSelfContainedKey[T any]() Option[T]

WithSelfContainedKey indicates that the key itself contains all the data. This option is only valid for string types (T must be string). When enabled:

  • No additional hash storage is used
  • The ZSET member (key) is the data itself
  • More efficient for simple string timers

Example usage:

manager := New[string](ctx, config, func(s string) string { return s }, handlers, WithSelfContainedKey[string]())

func WithUnmarshal

func WithUnmarshal[T any](fn func([]byte) (T, error)) Option[T]

WithUnmarshal sets custom unmarshal function

type RedisTimer

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

RedisTimer implements timer.Manager using Redis

func (*RedisTimer[T]) Cancel

func (r *RedisTimer[T]) Cancel(ctx context.Context, timerType string, key string) error

Cancel removes a timer for an item of a specific type

func (*RedisTimer[T]) Close

func (r *RedisTimer[T]) Close() error

Close releases resources and stops processing

func (*RedisTimer[T]) GetPending

func (r *RedisTimer[T]) GetPending(ctx context.Context, timerType string) (int64, error)

GetPending returns the count of pending timers for a specific type

func (*RedisTimer[T]) Register

func (r *RedisTimer[T]) Register(ctx context.Context, timerType string, item T, timeout time.Duration) error

Register adds an item with a timeout duration for a specific type If a timer with the same key already exists, it will be updated (upsert behavior)

func (*RedisTimer[T]) RegisterAt

func (r *RedisTimer[T]) RegisterAt(ctx context.Context, timerType string, item T, expireAt time.Time) error

RegisterAt adds an item that expires at a specific time for a specific type

func (*RedisTimer[T]) RegisterAtIfNotExists

func (r *RedisTimer[T]) RegisterAtIfNotExists(ctx context.Context, timerType string, item T, expireAt time.Time) (bool, error)

RegisterAtIfNotExists adds an item that expires at a specific time only if it doesn't exist Returns false if timer already exists, true if newly registered

func (*RedisTimer[T]) RegisterIfNotExists

func (r *RedisTimer[T]) RegisterIfNotExists(ctx context.Context, timerType string, item T, timeout time.Duration) (bool, error)

RegisterIfNotExists adds an item with a timeout duration only if it doesn't exist Returns false if timer already exists, true if newly registered

Jump to

Keyboard shortcuts

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