feat: add a redis abstraction to support different versions of the redis sdk (#549)

This commit is contained in:
Ryo
2025-08-05 16:09:54 +08:00
committed by GitHub
parent d4d0ba6b9a
commit ab0ce50df5
11 changed files with 316 additions and 31 deletions

View File

@@ -17,9 +17,96 @@
package cache
import (
"github.com/redis/go-redis/v9"
"context"
"time"
)
type Cmdable = redis.Cmdable
var Nil error
const Nil = redis.Nil
func SetDefaultNilError(err error) {
Nil = err
}
type Cmdable interface {
Pipeline() Pipeliner
StringCmdable
HashCmdable
GenericCmdable
ListCmdable
}
type StringCmdable interface {
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) StatusCmd
Get(ctx context.Context, key string) StringCmd
IncrBy(ctx context.Context, key string, value int64) IntCmd
Incr(ctx context.Context, key string) IntCmd
}
type HashCmdable interface {
HSet(ctx context.Context, key string, values ...interface{}) IntCmd
HGetAll(ctx context.Context, key string) MapStringStringCmd
}
type GenericCmdable interface {
Del(ctx context.Context, keys ...string) IntCmd
Exists(ctx context.Context, keys ...string) IntCmd
Expire(ctx context.Context, key string, expiration time.Duration) BoolCmd
}
type Pipeliner interface {
StatefulCmdable
Exec(ctx context.Context) ([]Cmder, error)
}
type StatefulCmdable interface {
Cmdable
}
type ListCmdable interface {
LIndex(ctx context.Context, key string, index int64) StringCmd
LPush(ctx context.Context, key string, values ...interface{}) IntCmd
RPush(ctx context.Context, key string, values ...interface{}) IntCmd
LSet(ctx context.Context, key string, index int64, value interface{}) StatusCmd
LPop(ctx context.Context, key string) StringCmd
LRange(ctx context.Context, key string, start, stop int64) StringSliceCmd
}
type Cmder interface {
Err() error
}
type baseCmd interface {
Err() error
}
type IntCmd interface {
baseCmd
Result() (int64, error)
}
type MapStringStringCmd interface {
baseCmd
Result() (map[string]string, error)
}
type BoolCmd interface {
baseCmd
Result() (bool, error)
}
type StatusCmd interface {
baseCmd
Result() (string, error)
}
type StringCmd interface {
baseCmd
Result() (string, error)
Val() string
Int64() (int64, error)
Bytes() ([]byte, error)
}
type StringSliceCmd interface {
baseCmd
Result() ([]string, error)
}