Skip to content

Commit 7bf2d10

Browse files
committed
♻️ refactor: updated struct redis #8
1 parent 7111d40 commit 7bf2d10

File tree

6 files changed

+90
-25
lines changed

6 files changed

+90
-25
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*.out
1616

1717
# Dependency directories (remove the comment below to include it)
18-
# vendor/
18+
vendor/
1919

2020
# Go workspace file
2121
go.work

Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.PHONY: run build test tidy deps-upgrade deps-clean-cache
2+
3+
# ==============================================================================
4+
# Start Rest
5+
run:
6+
go run main.go
7+
8+
build:
9+
go build main.go
10+
11+
# ==============================================================================
12+
# Modules support
13+
test:
14+
go test -cover ./...
15+
16+
tidy:
17+
go mod tidy
18+
go mod vendor
19+
20+
deps-upgrade:
21+
# go get $(go list -f '{{if not (or .Main .Indirect)}}{{.Path}}{{end}}' -m all)
22+
go get -u -t -d -v ./...
23+
go mod tidy
24+
go mod vendor
25+
26+
deps-clean-cache:
27+
go clean -modcache

go.mod

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ module github.com/sivaosorg/redisconn
22

33
go 1.20
44

5-
require github.com/sivaosorg/govm v1.2.6
5+
require github.com/sivaosorg/govm v1.2.8
66

77
require (
8-
github.com/fatih/color v1.15.0 // indirect
8+
github.com/fatih/color v1.16.0 // indirect
99
github.com/mattn/go-colorable v0.1.13 // indirect
10-
github.com/mattn/go-isatty v0.0.17 // indirect
10+
github.com/mattn/go-isatty v0.0.20 // indirect
1111
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
1212
github.com/onsi/ginkgo v1.16.5 // indirect
1313
github.com/onsi/gomega v1.27.8 // indirect
1414
github.com/sirupsen/logrus v1.9.3 // indirect
15-
golang.org/x/sys v0.12.0 // indirect
15+
golang.org/x/sys v0.15.0 // indirect
1616
)
1717

1818
require (
1919
github.com/go-redis/redis v6.15.9+incompatible
2020
github.com/json-iterator/go v1.1.12 // indirect
21-
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
21+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
2222
github.com/modern-go/reflect2 v1.0.2 // indirect
2323
)

go.sum

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8
22
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
44
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5-
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
6-
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
5+
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
6+
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
77
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
88
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
99
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
@@ -28,10 +28,11 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
2828
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
2929
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
3030
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
31-
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
32-
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
33-
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
31+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
32+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
3433
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
34+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
35+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
3536
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
3637
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
3738
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
@@ -51,10 +52,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
5152
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5253
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
5354
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
54-
github.com/sivaosorg/govm v1.2.3 h1:Hwt6SQjwW5XADWqvfwzlf3t9kr5seoPpMk8EE9MO+6I=
55-
github.com/sivaosorg/govm v1.2.3/go.mod h1:rXfPCNGc4ddPf1+VRX8Ytw/5xqehfPRrCr53Oi+cwpw=
56-
github.com/sivaosorg/govm v1.2.6 h1:BCUCNk/yy/rFZ+clMF7ftfw99ZQFO+4QSFV/xe2mWfA=
57-
github.com/sivaosorg/govm v1.2.6/go.mod h1:rXfPCNGc4ddPf1+VRX8Ytw/5xqehfPRrCr53Oi+cwpw=
55+
github.com/sivaosorg/govm v1.2.8 h1:SyVwq7PNUKQQNqI9PbjZ6zY9b33lks75y/qzAzxDNyI=
56+
github.com/sivaosorg/govm v1.2.8/go.mod h1:rXfPCNGc4ddPf1+VRX8Ytw/5xqehfPRrCr53Oi+cwpw=
5857
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5958
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
6059
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
@@ -85,8 +84,9 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
8584
golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8685
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8786
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
88-
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
89-
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
87+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
88+
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
89+
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
9090
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
9191
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
9292
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=

redisconn/redisconn.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,53 @@ import (
88
"github.com/sivaosorg/govm/dbx"
99
"github.com/sivaosorg/govm/logger"
1010
"github.com/sivaosorg/govm/redisx"
11+
"github.com/sivaosorg/govm/utils"
1112
)
1213

1314
var (
14-
instance *redis.Client
15+
instance *Redis
1516
_logger = logger.NewLogger()
1617
)
1718

18-
func NewClient(config redisx.RedisConfig) (*redis.Client, dbx.Dbx) {
19+
func NewRedis() *Redis {
20+
return &Redis{}
21+
}
22+
23+
func (r *Redis) SetConn(value *redis.Client) *Redis {
24+
r.conn = value
25+
return r
26+
}
27+
28+
func (r *Redis) SetConfig(value redisx.RedisConfig) *Redis {
29+
r.Config = value
30+
return r
31+
}
32+
33+
func (r *Redis) SetState(value dbx.Dbx) *Redis {
34+
r.State = value
35+
return r
36+
}
37+
38+
func (r *Redis) Json() string {
39+
return utils.ToJson(r)
40+
}
41+
42+
func (r *Redis) GetConn() *redis.Client {
43+
return r.conn
44+
}
45+
46+
func NewClient(config redisx.RedisConfig) (*Redis, dbx.Dbx) {
1947
s := dbx.NewDbx().SetDatabase(config.Database)
2048
if !config.IsEnabled {
2149
s.SetConnected(false).
2250
SetMessage("Redis unavailable").
2351
SetError(fmt.Errorf(s.Message))
24-
return &redis.Client{}, *s
52+
instance = NewRedis().SetState(*s)
53+
return instance, *s
2554
}
2655
if instance != nil {
27-
s.SetConnected(true)
56+
s.SetConnected(true).SetNewInstance(false)
57+
instance.SetState(*s)
2858
return instance, *s
2959
}
3060
client := redis.NewClient(&redis.Options{
@@ -36,14 +66,14 @@ func NewClient(config redisx.RedisConfig) (*redis.Client, dbx.Dbx) {
3666
err := client.Ping().Err()
3767
if err != nil {
3868
s.SetConnected(false).SetError(err).SetMessage(err.Error())
39-
return &redis.Client{}, *s
69+
instance = NewRedis().SetState(*s)
70+
return instance, *s
4071
}
4172
if config.DebugMode {
4273
_logger.Info(fmt.Sprintf("Redis client connection:: %s", config.Json()))
4374
_logger.Info(fmt.Sprintf("Connected successfully to redis cache:: %s/%s", config.UrlConn, config.Database))
4475
}
45-
instance = client
46-
pid := os.Getpid()
47-
s.SetConnected(true).SetMessage("Connection established").SetPid(pid).SetNewInstance(true)
76+
s.SetConnected(true).SetMessage("Connected successfully").SetPid(os.Getpid()).SetNewInstance(true)
77+
instance = NewRedis().SetConn(client).SetState(*s)
4878
return instance, *s
4979
}

redisconn/redisconn_model.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"sync"
55

66
"github.com/go-redis/redis"
7+
"github.com/sivaosorg/govm/dbx"
8+
"github.com/sivaosorg/govm/redisx"
79
)
810

911
type RedisPubSubClient struct {
@@ -14,3 +16,9 @@ type RedisPubSubClient struct {
1416
type RedisMutex struct {
1517
mutexes map[string]*sync.Mutex
1618
}
19+
20+
type Redis struct {
21+
conn *redis.Client `json:"-"`
22+
Config redisx.RedisConfig `json:"config,omitempty"`
23+
State dbx.Dbx `json:"state,omitempty"`
24+
}

0 commit comments

Comments
 (0)