Skip to content

Commit

Permalink
redis: allow accepting redis.Client directly (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Dec 15, 2023
1 parent c4519ed commit 9253f0c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
15 changes: 7 additions & 8 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type redisStore struct {
// newRedisStore returns a new Redis session store based on given configuration.
func newRedisStore(cfg Config) *redisStore {
return &redisStore{
client: cfg.client,
client: cfg.Client,
keyPrefix: cfg.KeyPrefix,
lifetime: cfg.Lifetime,
encoder: cfg.Encoder,
Expand Down Expand Up @@ -92,9 +92,9 @@ type Options = redis.Options

// Config contains options for the Redis session store.
type Config struct {
// For tests only
client *redis.Client

// Client is the Redis Client connection. If not set, a new client will be
// created based on Options.
Client *redis.Client
// Options is the settings to set up Redis client connection.
Options *Options
// KeyPrefix is the prefix to use for keys in Redis. Default is "session:".
Expand All @@ -121,14 +121,13 @@ func Initer() session.Initer {

if cfg == nil {
return nil, fmt.Errorf("config object with the type '%T' not found", Config{})
} else if cfg.Options == nil && cfg.client == nil {
} else if cfg.Options == nil && cfg.Client == nil {
return nil, errors.New("empty Options")
}

if cfg.client == nil {
cfg.client = redis.NewClient(cfg.Options)
if cfg.Client == nil {
cfg.Client = redis.NewClient(cfg.Options)
}

if cfg.KeyPrefix == "" {
cfg.KeyPrefix = "session:"
}
Expand Down
6 changes: 3 additions & 3 deletions redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestRedisStore(t *testing.T) {
session.Options{
Initer: Initer(),
Config: Config{
client: client,
Client: client,
},
},
))
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestRedisStore_GC(t *testing.T) {

store, err := Initer()(ctx,
Config{
client: client,
Client: client,
Lifetime: time.Second,
},
)
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestRedisStore_Touch(t *testing.T) {

store, err := Initer()(ctx,
Config{
client: client,
Client: client,
Lifetime: time.Second,
},
)
Expand Down

0 comments on commit 9253f0c

Please sign in to comment.