Skip to content

Commit 02d06ae

Browse files
authored
✨ add possibility to select database
1 parent 7bb3d02 commit 02d06ae

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import simpleredis "github.com/maxlerebourg/simpleredis"
99

1010
var redis simpleredis.SimpleRedis
1111

12-
redis.Init("redis:6379", "") // redisHost, redisPass
12+
redis.Init("redis:6379", "", "") // redisHost, redisPass, redisDatabase
1313

1414
err := redis.Set("test", []bytes("whatever"), 60), // Set key "test" with "whatever" for 60 seconds
1515
if err != nil {

simpleredis.go

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ type redisCmd struct {
3131

3232
// A SimpleRedis is used to communicate with redis.
3333
type SimpleRedis struct {
34-
host string
35-
pass string
34+
host string
35+
pass string
36+
database string
3637
}
3738

3839
func genRedisArray(params ...[]byte) []byte {
@@ -51,7 +52,25 @@ func send(wr *textproto.Writer, method string, data []byte) {
5152
}
5253
}
5354

54-
func askRedis(sr *SimpleRedis, cmd redisCmd, channel chan redisCmd) {
55+
func (sr *SimpleRedis) waitRedis(reader *textproto.Reader, channel chan redisCmd) {
56+
for {
57+
select {
58+
case <-time.After(time.Second * 1):
59+
channel <- redisCmd{Error: fmt.Errorf(RedisTimeout)}
60+
return
61+
default:
62+
read, _ := reader.ReadLineBytes()
63+
if string(read) != "+OK" {
64+
channel <- redisCmd{Error: fmt.Errorf(RedisNoAuth)}
65+
return
66+
}
67+
}
68+
// breaks out of for
69+
break
70+
}
71+
}
72+
73+
func (sr *SimpleRedis) askRedis(cmd redisCmd, channel chan redisCmd) {
5574
dialer := net.Dialer{Timeout: 2 * time.Second}
5675
conn, err := dialer.Dial("tcp", sr.host)
5776
if err != nil {
@@ -70,21 +89,13 @@ func askRedis(sr *SimpleRedis, cmd redisCmd, channel chan redisCmd) {
7089
if sr.pass != "" {
7190
data := genRedisArray([]byte("AUTH"), []byte(sr.pass))
7291
send(writer, "auth", data)
73-
for {
74-
select {
75-
case <-time.After(time.Second * 1):
76-
channel <- redisCmd{Error: fmt.Errorf(RedisTimeout)}
77-
return
78-
default:
79-
read, _ := reader.ReadLineBytes()
80-
if string(read) != "+OK" {
81-
channel <- redisCmd{Error: fmt.Errorf(RedisNoAuth)}
82-
return
83-
}
84-
}
85-
// breaks out of for
86-
break
87-
}
92+
sr.waitRedis(reader, channel)
93+
}
94+
95+
if sr.database != "" {
96+
data := genRedisArray([]byte("SELECT"), []byte(sr.database))
97+
send(writer, "select", data)
98+
sr.waitRedis(reader, channel)
8899
}
89100

90101
switch cmd.Command {
@@ -121,9 +132,10 @@ func askRedis(sr *SimpleRedis, cmd redisCmd, channel chan redisCmd) {
121132
}
122133

123134
// Init sets the redisHost used to connect to redis.
124-
func (sr *SimpleRedis) Init(host string, pass string) {
135+
func (sr *SimpleRedis) Init(host, pass, database string) {
125136
sr.host = host
126137
sr.pass = pass
138+
sr.database = database
127139
}
128140

129141
// Get fetches the value for key name in redis.
@@ -133,7 +145,7 @@ func (sr *SimpleRedis) Get(name string) ([]byte, error) {
133145
Name: name,
134146
}
135147
channel := make(chan redisCmd)
136-
go askRedis(sr, cmd, channel)
148+
go sr.askRedis(cmd, channel)
137149
resp := <-channel
138150
if resp.Error != nil {
139151
return nil, resp.Error
@@ -149,7 +161,7 @@ func (sr *SimpleRedis) Set(name string, data []byte, duration int64) error {
149161
Data: data,
150162
Duration: duration,
151163
}
152-
go askRedis(sr, cmd, nil)
164+
go sr.askRedis(cmd, nil)
153165
return nil
154166
}
155167

@@ -159,6 +171,6 @@ func (sr *SimpleRedis) Del(name string) error {
159171
Command: "DEL",
160172
Name: name,
161173
}
162-
go askRedis(sr, cmd, nil)
174+
go sr.askRedis(cmd, nil)
163175
return nil
164176
}

0 commit comments

Comments
 (0)