Browse Source

Merge pull request #134 from lsytj0413/optimize-redis-get

refactor(*): mv common.RedisGet to storage/redis
pull/136/head
yedf2 4 years ago
committed by GitHub
parent
commit
74170ec8b2
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      common/types.go
  2. 1
      common/types_test.go
  3. 21
      dtmsvr/storage/redis/redis.go

30
common/types.go

@ -1,30 +0,0 @@
/*
* Copyright (c) 2021 yedf. All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file.
*/
package common
import (
"fmt"
"sync"
"github.com/dtm-labs/dtm/dtmcli/logger"
"github.com/go-redis/redis/v8"
)
var rdb *redis.Client
var once sync.Once
func RedisGet() *redis.Client {
once.Do(func() {
logger.Debugf("connecting to redis: %v", Config.Store)
rdb = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", Config.Store.Host, Config.Store.Port),
Username: Config.Store.User,
Password: Config.Store.Password,
})
})
return rdb
}

1
common/types_test.go

@ -20,6 +20,7 @@ func TestGeneralDB(t *testing.T) {
testDbAlone(t)
}
}
func testSql(t *testing.T) {
db := DbGet(Config.Store.GetDBConf())
err := func() (rerr error) {

21
dtmsvr/storage/redis/redis.go

@ -3,6 +3,7 @@ package redis
import (
"context"
"fmt"
"sync"
"time"
"github.com/go-redis/redis/v8"
@ -14,10 +15,13 @@ import (
"github.com/dtm-labs/dtm/dtmsvr/storage"
)
// TODO: optimize this, it's very strange to use pointer to common.Config
var config = &common.Config
var ctx context.Context = context.Background()
// TODO: optimize this, all function should have context as first parameter
var ctx = context.Background()
// RedisStore is the storage with redis, all transaction information will bachend with redis
type RedisStore struct {
}
@ -266,6 +270,19 @@ redis.call('SET', KEYS[1], ARGV[3], 'EX', ARGV[2])
dtmimp.E2P(err)
}
var (
rdb *redis.Client
once sync.Once
)
func redisGet() *redis.Client {
return common.RedisGet()
once.Do(func() {
logger.Debugf("connecting to redis: %v", config.Store)
rdb = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", config.Store.Host, config.Store.Port),
Username: config.Store.User,
Password: config.Store.Password,
})
})
return rdb
}

Loading…
Cancel
Save