Skip to content

Commit

Permalink
Merge pull request #69 from qedus/redis-remove-global-var
Browse files Browse the repository at this point in the history
Redis remove global var and some housekeeping
  • Loading branch information
someone1 authored Jan 22, 2020
2 parents 4b0cefc + 9e1e88a commit 57e8cbc
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ sudo: false
go:
- 1.11.x
- 1.12.x
- 1.13.x
env:
- REDIS_ADDR="localhost:6379" APPENGINE_DEV_APPSERVER="$HOME/google-cloud-sdk/bin/dev_appserver.py" GO111MODULE="on" DATASTORE_EMULATOR_HOST="localhost:8432" DATASTORE_PROJECT_ID="nds-test"
services:
Expand All @@ -16,7 +17,7 @@ install:
- "$HOME/google-cloud-sdk/bin/gcloud components install app-engine-python --quiet"
- "$HOME/google-cloud-sdk/bin/gcloud components install app-engine-go --quiet"
- go get -v -d -t ./...
- go get github.com/golangci/golangci-lint/cmd/golangci-lint
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.23.1
before_script:
- "$HOME/google-cloud-sdk/bin/gcloud config set project nds-test"
- "$HOME/google-cloud-sdk/bin/gcloud beta emulators datastore start --quiet --no-store-on-disk --consistency=1 --host-port=localhost:8432 &"
Expand Down
23 changes: 11 additions & 12 deletions cachers/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ const (
end`
)

var (
casSha = ""
)

// NewCacher will return a nds.Cacher backed by
// the provided redis pool. It will try and load a script
// into the redis script cache and return an error if it is
Expand All @@ -54,17 +50,20 @@ func NewCacher(ctx context.Context, pool *redis.Pool) (n nds.Cacher, err error)
}
}()

if casSha, err = redis.String(conn.DoContext(ctx, "SCRIPT", "LOAD", casScript)); err != nil {
b := backend{store: pool}

if b.casSha, err = redis.String(conn.DoContext(ctx, "SCRIPT", "LOAD", casScript)); err != nil {
return
}

n = &backend{store: pool}
n = &b

return
}

type backend struct {
store *redis.Pool
store *redis.Pool
casSha string
}

var bufPool = sync.Pool{
Expand Down Expand Up @@ -102,7 +101,7 @@ func set(ctx context.Context, conn redis.ConnWithContext, nx bool, items []*nds.
defer close(meChan)

buf := bufPool.Get().(*bytes.Buffer)
Loop:
Loop:
for _, item := range items {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -136,7 +135,7 @@ Loop:

go func() {
defer wg.Done()
Loop2:
Loop2:
for i := 0; i < len(items); i++ {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -199,7 +198,7 @@ func (b *backend) CompareAndSwapMulti(ctx context.Context, items []*nds.Item) (e
defer close(meChan)

buf := bufPool.Get().(*bytes.Buffer)
Loop:
Loop:
for _, item := range items {
select {
case <-ctx.Done():
Expand All @@ -215,7 +214,7 @@ Loop:
if item.Expiration == 0 {
expire = -1
}
if rerr := redisConn.SendContext(ctx, "EVALSHA", casSha, "1", item.Key, cas, buf.Bytes(), expire); rerr != nil {
if rerr := redisConn.SendContext(ctx, "EVALSHA", b.casSha, "1", item.Key, cas, buf.Bytes(), expire); rerr != nil {
meChan <- rerr
}
} else {
Expand All @@ -230,7 +229,7 @@ Loop:

go func() {
defer wg.Done()
Loop2:
Loop2:
for i := 0; i < len(items); i++ {
select {
case <-ctx.Done():
Expand Down
23 changes: 14 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
module github.com/qedus/nds/v2

require (
cloud.google.com/go v0.43.0
github.com/hashicorp/golang-lru v0.5.3 // indirect
cloud.google.com/go v0.51.0 // indirect
cloud.google.com/go/datastore v1.0.0
cloud.google.com/go/storage v1.5.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/google/go-cmp v0.4.0 // indirect
github.com/opencensus-integrations/redigo v2.0.1+incompatible
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/pkg/errors v0.8.1
go.opencensus.io v0.22.0
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80 // indirect
golang.org/x/sys v0.0.0-20190801041406-cbf593c0f2f3 // indirect
google.golang.org/appengine v1.6.1
google.golang.org/genproto v0.0.0-20190801165951-fa694d86fc64 // indirect
google.golang.org/grpc v1.22.1 // indirect
github.com/pkg/errors v0.9.1
go.opencensus.io v0.22.2
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a // indirect
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20200121082415-34d275377bf9 // indirect
golang.org/x/tools v0.0.0-20200121230703-78d067421b02 // indirect
google.golang.org/appengine v1.6.5
google.golang.org/genproto v0.0.0-20200117163144-32f20d992d24 // indirect
)
Loading

0 comments on commit 57e8cbc

Please sign in to comment.