xredis

package module
v0.0.0-...-587cc14 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2023 License: MIT Imports: 12 Imported by: 0

README

xredis

Dependencies

  • github.com/Aoi-hosizora/ahlib
  • github.com/go-redis/redis/v8
  • github.com/sirupsen/logrus

Documents

Types
  • type LoggerOption func
  • type ILogger interface
  • type SilenceLogger struct
  • type LogrusLogger struct
  • type StdLogger struct
  • type LoggerParam struct
Variables
  • var FormatLoggerFunc func
  • var FieldifyLoggerFunc func
Constants
  • None
Functions
  • func ScanAll(ctx context.Context, client *redis.Client, match string, count int64) (keys []string, err error)
  • func ScanAllWithCallback(ctx context.Context, client *redis.Client, match string, count int64, callback func(keys []string)) error
  • func DelAll(ctx context.Context, client *redis.Client, pattern string) (tot int64, err error)
  • func DelAllByScan(ctx context.Context, client *redis.Client, pattern string, scanCount int64) (tot int64, err error)
  • func DelAllByScanCallback(ctx context.Context, client *redis.Client, pattern string, scanCount int64, ignoreDelError bool) (tot int64, err error)
  • func WithLogErr(log bool) LoggerOption
  • func WithLogCmd(log bool) LoggerOption
  • func WithSkip(skip int) LoggerOption
  • func WithSlowThreshold(threshold time.Duration) LoggerOption
  • func EnableLogger()
  • func DisableLogger()
  • func NewSilenceLogger() *SilenceLogger
  • func NewLogrusLogger(logger *logrus.Logger, options ...LoggerOption) *LogrusLogger
  • func NewStdLogger(logger logrus.StdLogger, options ...LoggerOption) *StdLogger
Methods
  • func (s *SilenceLogger) Printf(context.Context, string, ...interface{})
  • func (l *LogrusLogger) BeforeProcess(ctx context.Context, _ redis.Cmder) (context.Context, error)
  • func (l *LogrusLogger) AfterProcess(ctx context.Context, cmd redis.Cmder) error
  • func (l *StdLogger) BeforeProcess(ctx context.Context, _ redis.Cmder) (context.Context, error)
  • func (l *StdLogger) AfterProcess(ctx context.Context, cmd redis.Cmder) error

Documentation

Index

Constants

View Source
const (
	// NAME represents ahlib-mx/xredis package's name.
	NAME = "ahlib-mx/xredis"

	// VERSION represents ahlib-mx/xredis package's current version.
	VERSION = "1.6.0"
)

Variables

View Source
var (
	// FormatLoggerFunc is a custom LoggerParam's format function for LogrusLogger and StdLogger.
	FormatLoggerFunc func(p *LoggerParam) string

	// FieldifyLoggerFunc is a custom LoggerParam's fieldify function for LogrusLogger.
	FieldifyLoggerFunc func(p *LoggerParam) logrus.Fields
)

Functions

func DelAll

func DelAll(ctx context.Context, client *redis.Client, pattern string) (tot int64, err error)

DelAll atomically deletes all keys from given pattern, using KEYS command to get all keys before DEL command.

func DelAllByScan

func DelAllByScan(ctx context.Context, client *redis.Client, pattern string, scanCount int64) (tot int64, err error)

DelAllByScan atomically deletes all keys from given pattern, using SCAN command to get all keys before DEL command.

func DelAllByScanCallback

func DelAllByScanCallback(ctx context.Context, client *redis.Client, pattern string, scanCount int64, ignoreDelError bool) (tot int64, err error)

DelAllByScanCallback atomically deletes all keys from given pattern, using SCAN command with callback and DEL command multi times.

func DisableLogger

func DisableLogger()

DisableLogger disables LogrusLogger and StdLogger.

func EnableLogger

func EnableLogger()

EnableLogger enables LogrusLogger and StdLogger to do any log.

func ScanAll

func ScanAll(ctx context.Context, client *redis.Client, match string, count int64) (keys []string, err error)

ScanAll scans all keys from given pattern and scan count, and returns all keys when finish scanning, is a wrapper function for SCAN command.

func ScanAllWithCallback

func ScanAllWithCallback(ctx context.Context, client *redis.Client, match string, count int64, callback func(keys []string, cursor uint64) (toContinue bool)) error

ScanAllWithCallback scans all keys from given pattern and scan count, and invokes callback when get some keys, is a wrapper function for SCAN command.

Types

type ILogger

type ILogger interface {
	Printf(ctx context.Context, format string, v ...interface{})
}

ILogger abstracts redis's internal logger to an interface.

type LoggerOption

type LoggerOption func(*loggerOptions)

LoggerOption represents an option type for LogrusLogger and StdLogger's option, can be created by WithXXX functions.

func WithLogCmd

func WithLogCmd(log bool) LoggerOption

WithLogCmd creates a LoggerOption to decide whether to do log for redis commands or not, defaults to true.

func WithLogErr

func WithLogErr(log bool) LoggerOption

WithLogErr creates a LoggerOption to decide whether to do log for errors or not, defaults to true.

func WithSkip

func WithSkip(skip int) LoggerOption

WithSkip creates a LoggerOption to specify runtime skip for getting runtime information, defaults to 4.

func WithSlowThreshold

func WithSlowThreshold(threshold time.Duration) LoggerOption

WithSlowThreshold creates a LoggerOption to specify a slow operation's duration used to highlight loggers, defaults to 0ms, means no highlight.

type LoggerParam

type LoggerParam struct {
	Command  string
	Rows     int64
	Status   string
	Duration time.Duration
	Slow     bool
	Source   string
	ErrorMsg string
}

LoggerParam stores some logger parameters and is used by LogrusLogger and StdLogger.

type LogrusLogger

type LogrusLogger struct {
	// contains filtered or unexported fields
}

LogrusLogger represents a redis.Hook as redis's logger, used to log redis's executing message to logrus.Logger.

func NewLogrusLogger

func NewLogrusLogger(logger *logrus.Logger, options ...LoggerOption) *LogrusLogger

NewLogrusLogger creates a new LogrusLogger using given logrus.Logger and LoggerOption-s.

Example:

client := redis.NewClient(...)
redis.SetLogger(NewSilenceLogger()) // must silence fist
l := logrus.New()
l.SetFormatter(&logrus.TextFormatter{})
client.AddHook(xredis.NewLogrusLogger(l))

func (*LogrusLogger) AfterProcess

func (l *LogrusLogger) AfterProcess(ctx context.Context, cmd redis.Cmder) error

AfterProcess implements redis.Hook interface, it logs redis's message to logrus.Logger.

func (*LogrusLogger) AfterProcessPipeline

func (*LogrusLogger) AfterProcessPipeline(context.Context, []redis.Cmder) error

AfterProcessPipeline implements redis.Hook interface.

func (*LogrusLogger) BeforeProcess

func (l *LogrusLogger) BeforeProcess(ctx context.Context, _ redis.Cmder) (context.Context, error)

BeforeProcess implements redis.Hook interface, it saves start time to context, and will be used in AfterProcess.

func (*LogrusLogger) BeforeProcessPipeline

func (*LogrusLogger) BeforeProcessPipeline(ctx context.Context, _ []redis.Cmder) (context.Context, error)

BeforeProcessPipeline implements redis.Hook interface.

type SilenceLogger

type SilenceLogger struct{}

SilenceLogger represents a redis's logger, used to hide all logs, including error message.

func NewSilenceLogger

func NewSilenceLogger() *SilenceLogger

NewSilenceLogger creates a new SilenceLogger.

Example:

client := redis.NewClient(options)
redis.SetLogger(xredis.NewSilenceLogger())

func (*SilenceLogger) Printf

func (s *SilenceLogger) Printf(context.Context, string, ...interface{})

Printf implements redis.Logging interface, it does nothing for logging.

type StdLogger

type StdLogger struct {
	// contains filtered or unexported fields
}

StdLogger represents a redis.Hook as redis's logger, used to log redis's executing message to logrus.StdLogger.

func NewStdLogger

func NewStdLogger(logger logrus.StdLogger, options ...LoggerOption) *StdLogger

NewStdLogger creates a new StdLogger using given logrus.StdLogger and LoggerOption-s.

Example:

client := redis.NewClient(...)
redis.SetLogger(NewSilenceLogger()) // must silence fist
l := log.Default()
client.AddHook(xredis.NewStdLogger(l))

func (*StdLogger) AfterProcess

func (l *StdLogger) AfterProcess(ctx context.Context, cmd redis.Cmder) error

AfterProcess implements redis.Hook interface, it logs redis's message to logrus.StdLogger.

func (*StdLogger) AfterProcessPipeline

func (*StdLogger) AfterProcessPipeline(context.Context, []redis.Cmder) error

AfterProcessPipeline implements redis.Hook interface.

func (*StdLogger) BeforeProcess

func (l *StdLogger) BeforeProcess(ctx context.Context, _ redis.Cmder) (context.Context, error)

BeforeProcess implements redis.Hook interface, it saves start time to context, and will be used in AfterProcess.

func (*StdLogger) BeforeProcessPipeline

func (*StdLogger) BeforeProcessPipeline(ctx context.Context, _ []redis.Cmder) (context.Context, error)

BeforeProcessPipeline implements redis.Hook interface.

Jump to

Keyboard shortcuts

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