add redis

This commit is contained in:
bryanqiu 2023-03-03 12:21:55 +08:00
parent 821fc1d763
commit adb92a63c3

View File

@ -20,14 +20,16 @@ import (
"fmt" "fmt"
"log" "log"
"time" "time"
"github.com/gomodule/redigo/redis"
) )
var ( var (
_ ConnWithTimeout = (*loggingConn)(nil) _ redis.ConnWithTimeout = (*loggingConn)(nil)
) )
// NewLoggingConn returns a logging wrapper around a connection. // NewLoggingConn returns a logging wrapper around a connection.
func NewLoggingConn(conn Conn, logger *log.Logger, prefix string) Conn { func NewLoggingConn(conn redis.Conn, logger *log.Logger, prefix string) redis.Conn {
if prefix != "" { if prefix != "" {
prefix = prefix + "." prefix = prefix + "."
} }
@ -35,7 +37,7 @@ func NewLoggingConn(conn Conn, logger *log.Logger, prefix string) Conn {
} }
// NewLoggingConnFilter returns a logging wrapper around a connection and a filter function. // NewLoggingConnFilter returns a logging wrapper around a connection and a filter function.
func NewLoggingConnFilter(conn Conn, logger *log.Logger, prefix string, skip func(cmdName string) bool) Conn { func NewLoggingConnFilter(conn redis.Conn, logger *log.Logger, prefix string, skip func(cmdName string) bool) redis.Conn {
if prefix != "" { if prefix != "" {
prefix = prefix + "." prefix = prefix + "."
} }
@ -43,7 +45,7 @@ func NewLoggingConnFilter(conn Conn, logger *log.Logger, prefix string, skip fun
} }
type loggingConn struct { type loggingConn struct {
Conn redis.Conn
logger *log.Logger logger *log.Logger
prefix string prefix string
skip func(cmdName string) bool skip func(cmdName string) bool
@ -123,13 +125,13 @@ func (c *loggingConn) Do(commandName string, args ...interface{}) (interface{},
} }
func (c *loggingConn) DoContext(ctx context.Context, commandName string, args ...interface{}) (interface{}, error) { func (c *loggingConn) DoContext(ctx context.Context, commandName string, args ...interface{}) (interface{}, error) {
reply, err := DoContext(c.Conn, ctx, commandName, args...) reply, err := redis.DoContext(c.Conn, ctx, commandName, args...)
c.print("DoContext", commandName, args, reply, err) c.print("DoContext", commandName, args, reply, err)
return reply, err return reply, err
} }
func (c *loggingConn) DoWithTimeout(timeout time.Duration, commandName string, args ...interface{}) (interface{}, error) { func (c *loggingConn) DoWithTimeout(timeout time.Duration, commandName string, args ...interface{}) (interface{}, error) {
reply, err := DoWithTimeout(c.Conn, timeout, commandName, args...) reply, err := redis.DoWithTimeout(c.Conn, timeout, commandName, args...)
c.print("DoWithTimeout", commandName, args, reply, err) c.print("DoWithTimeout", commandName, args, reply, err)
return reply, err return reply, err
} }
@ -147,13 +149,13 @@ func (c *loggingConn) Receive() (interface{}, error) {
} }
func (c *loggingConn) ReceiveContext(ctx context.Context) (interface{}, error) { func (c *loggingConn) ReceiveContext(ctx context.Context) (interface{}, error) {
reply, err := ReceiveContext(c.Conn, ctx) reply, err := redis.ReceiveContext(c.Conn, ctx)
c.print("ReceiveContext", "", nil, reply, err) c.print("ReceiveContext", "", nil, reply, err)
return reply, err return reply, err
} }
func (c *loggingConn) ReceiveWithTimeout(timeout time.Duration) (interface{}, error) { func (c *loggingConn) ReceiveWithTimeout(timeout time.Duration) (interface{}, error) {
reply, err := ReceiveWithTimeout(c.Conn, timeout) reply, err := redis.ReceiveWithTimeout(c.Conn, timeout)
c.print("ReceiveWithTimeout", "", nil, reply, err) c.print("ReceiveWithTimeout", "", nil, reply, err)
return reply, err return reply, err
} }