diff --git a/env.test b/env.test index ad66e16..abd272b 100644 --- a/env.test +++ b/env.test @@ -22,3 +22,8 @@ if ! (sudo systemctl status mysqld 1>/dev/null); then echo "starting mysqld" sudo systemctl start mysqld fi + +if ! (sudo systemctl status redis 1>/dev/null); then + echo "starting redis" + sudo systemctl start redis +fi diff --git a/go.mod b/go.mod index 222b2b9..fe1fc49 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,10 @@ require ( github.com/boombuler/barcode v1.0.0 // indirect github.com/go-sql-driver/mysql v1.4.0 github.com/jmoiron/sqlx v1.2.0 + github.com/mediocregopher/radix/v3 v3.3.1-0.20190716044027-c72935c74ab3 github.com/pquerna/otp v1.1.0 github.com/stretchr/testify v1.3.0 - golang.org/x/net v0.0.0-20190213061140-3a22650c66bd + golang.org/x/net v0.0.0-20190213061140-3a22650c66bd // indirect google.golang.org/api v0.1.0 google.golang.org/grpc v1.18.0 gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce diff --git a/go.sum b/go.sum index 1fafa4b..43f1364 100644 --- a/go.sum +++ b/go.sum @@ -62,6 +62,8 @@ github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/mattn/go-sqlite3 v1.9.0 h1:pDRiWfl+++eC2FEFRy6jXmQlvp4Yh3z1MJKg4UeYM/4= github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mediocregopher/radix/v3 v3.3.1-0.20190716044027-c72935c74ab3 h1:UjRk86epRQI7V3yFJaR7nYiahIoCMLI/1YjtH5ANfkw= +github.com/mediocregopher/radix/v3 v3.3.1-0.20190716044027-c72935c74ab3/go.mod h1:RsC7cELtyL4TGkg0nwRPTa+J2TXZ0dh/ruohD3rnjMk= github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4= github.com/neelance/astrewrite v0.0.0-20160511093645-99348263ae86/go.mod h1:kHJEU3ofeGjhHklVoIGuVj85JJwZ6kWPaJwCIxgnFmo= github.com/neelance/sourcemap v0.0.0-20151028013722-8c68805598ab/go.mod h1:Qr6/a/Q4r9LP1IltGz7tA7iOK1WonHEYhu1HRBA7ZiM= @@ -139,6 +141,8 @@ golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181030000716-a0a13e073c7b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522 h1:bhOzK9QyoD0ogCnFro1m2mz41+Ib0oOhfJnBp5MR4K4= +golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0= google.golang.org/api v0.1.0 h1:K6z2u68e86TPdSdefXdzvXgR1zEMa+459vBSfWYAZkI= diff --git a/mdb/mredis/redis.go b/mdb/mredis/redis.go new file mode 100644 index 0000000..77211d9 --- /dev/null +++ b/mdb/mredis/redis.go @@ -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, + } +} diff --git a/mdb/mredis/redis_test.go b/mdb/mredis/redis_test.go new file mode 100644 index 0000000..ba501a4 --- /dev/null +++ b/mdb/mredis/redis_test.go @@ -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") + } + }) +}