parent
d54954cd8e
commit
0e64f16f03
@ -0,0 +1,50 @@ |
||||
// Package mredis implements connecting to a redis instance.
|
||||
package mredis |
||||
|
||||
import ( |
||||
"context" |
||||
|
||||
"github.com/mediocregopher/mediocre-go-lib/mcfg" |
||||
"github.com/mediocregopher/mediocre-go-lib/mcmp" |
||||
"github.com/mediocregopher/mediocre-go-lib/mlog" |
||||
"github.com/mediocregopher/mediocre-go-lib/mrun" |
||||
"github.com/mediocregopher/radix/v3" |
||||
) |
||||
|
||||
// Redis is a wrapper around a redis client which provides more functionality.
|
||||
type Redis struct { |
||||
radix.Client |
||||
cmp *mcmp.Component |
||||
} |
||||
|
||||
// InstRedis instantiates a Redis instance which will be initialized when the
|
||||
// Init event is triggered on the given Component. The redis client will have
|
||||
// Close called on it when the Shutdown event is triggered on the given
|
||||
// Component.
|
||||
func InstRedis(parent *mcmp.Component) *Redis { |
||||
cmp := parent.Child("redis") |
||||
client := new(struct{ radix.Client }) |
||||
|
||||
addr := mcfg.String(cmp, "addr", |
||||
mcfg.ParamDefault("127.0.0.1:6379"), |
||||
mcfg.ParamUsage("Address redis is listening on")) |
||||
poolSize := mcfg.Int(cmp, "pool-size", |
||||
mcfg.ParamDefault(4), |
||||
mcfg.ParamUsage("Number of connections in pool")) |
||||
mrun.InitHook(cmp, func(ctx context.Context) error { |
||||
cmp.Annotate("addr", *addr, "poolSize", *poolSize) |
||||
mlog.From(cmp).Info("connecting to redis", ctx) |
||||
var err error |
||||
client.Client, err = radix.NewPool("tcp", *addr, *poolSize) |
||||
return err |
||||
}) |
||||
mrun.ShutdownHook(cmp, func(ctx context.Context) error { |
||||
mlog.From(cmp).Info("shutting down redis", ctx) |
||||
return client.Close() |
||||
}) |
||||
|
||||
return &Redis{ |
||||
Client: client, |
||||
cmp: cmp, |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
package mredis |
||||
|
||||
import ( |
||||
. "testing" |
||||
|
||||
"github.com/mediocregopher/mediocre-go-lib/mtest" |
||||
|
||||
"github.com/mediocregopher/radix/v3" |
||||
) |
||||
|
||||
func TestRedis(t *T) { |
||||
cmp := mtest.Component() |
||||
redis := InstRedis(cmp) |
||||
mtest.Run(cmp, t, func() { |
||||
var info string |
||||
if err := redis.Do(radix.Cmd(&info, "INFO")); err != nil { |
||||
t.Fatal(err) |
||||
} else if len(info) < 0 { |
||||
t.Fatal("empty info return") |
||||
} |
||||
}) |
||||
} |
Loading…
Reference in new issue