go-models-redis
its lite and easy model.
* Go 1.12 or higher.
* [go-redis/redis](https://github.com/go-redis/redis) package
create go.mod
file in your package folder, and fill below
module github.com/eehsiao/go-models-example
go 1.13
require (
github.com/eehsiao/go-models-lib latest
github.com/eehsiao/go-models-redis latest
github.com/go-redis/redis v6.15.5+incompatible
)
Easy to start the test evn. That you can run the example code.
$ docker-compose up -d
import (
"database/sql"
"fmt"
mysql "github.com/eehsiao/go-models-mysql"
redis "github.com/eehsiao/go-models-redis"
)
// User : json struct that to store into redis
type User struct {
Host string `json:"host"`
User string `json:"user"`
SelectPriv string `json:"select_priv"`
}
//new redis dao
redUserModel := &RedUserModel{
Dao: redis.NewDao().SetConfig("127.0.0.1:6379", "", 0).OpenDB(),
}
// set a struct for dao as default model (option)
// (*User)(nil) : nil pointer of the User struct
// "user" : is real table name in the db
SetDefaultModel((*User)(nil), "user")
The example will connect to local mysql and get user data. Then connect to local redis and set user data, and get back.
https://github.com/eehsiao/go-models-example/
How to design model data logical
create a data struct, and add the tag json:"name"
type User struct {
Host string `json:"host"`
User string `json:"user"`
SelectPriv string `json:"select_priv"`
IntVal int `json:"user,string"`
}
if you have integer value, you can add a transfer type desc.
such as json:"user,string
"
create redis dao
m := redis.NewDao().SetConfig("127.0.0.1:6379", "", 0).OpenDB()
directly use the go-redis command function
redBool, err = m.HSet(userTable, redKey, serialStr).Result()