Skip to content

Commit

Permalink
add redis
Browse files Browse the repository at this point in the history
  • Loading branch information
yifanshaoye committed Jun 3, 2022
1 parent 55b3c20 commit d86a965
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/yifanshaoye/ctdao
go 1.18

require (
github.com/go-redis/redis v6.15.9+incompatible // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
Expand Down
34 changes: 34 additions & 0 deletions goredis/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package goredis

import (
"errors"
"fmt"
"github.com/go-redis/redis"
)

// dsn: user:password@tcp(ip:port)/dbname?charset=utf8mb4&parseTime=True&loc=Local
func GetRedisInstance(host, port, password string) (*redis.Client, error) {
if len(host) == 0 {
host = "127.0.0.1"
}
if len(port) == 0 {
port = "6379"
}
addr := host + ":" + port
client := redis.NewClient(&redis.Options{
Addr: addr,
Password: password,
})

for i := 0; i < 3; i++ {
if pong, err := client.Ping().Result(); err != nil {
fmt.Printf("connect redis %s, error: %+v\n", addr + " " + password, err)
continue
} else {
fmt.Println("connect redis ok: ", pong)
return client, nil
}
}

return client, errors.New("redis connect fails")
}
22 changes: 15 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"github.com/yifanshaoye/ctdao/gorm"
"github.com/yifanshaoye/ctdao/goredis"
)

type User struct {
Expand All @@ -12,12 +12,20 @@ type User struct {

func main() {
fmt.Println("Hello, World !!!")
gorm.Done()
dsn := "root:long123456@tcp(127.0.0.1:3306)/collections?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.GetMysqlInstance(dsn)
fmt.Println("err: ", err)
user := []User{{Name: "teng"}, {Name: "chen"}}
db.Create(user)

rcli,_ := goredis.GetRedisInstance("", "", "")
res := rcli.Get("a")
fmt.Println( res)

res = rcli.Get("b")
fmt.Println( res)

//gorm.Done()
//dsn := "root:long123456@tcp(127.0.0.1:3306)/collections?charset=utf8mb4&parseTime=True&loc=Local"
//db, err := gorm.GetMysqlInstance(dsn)
//fmt.Println("err: ", err)
//user := []User{{Name: "teng"}, {Name: "chen"}}
//db.Create(user)

//zaplog.SetLogFilePath("./database/access.log")
//zaplog.InitZaplog()
Expand Down

0 comments on commit d86a965

Please sign in to comment.