diff --git a/go.mod b/go.mod index cc7782d..8d73f7e 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/gin-gonic/gin v1.7.4 + github.com/gomodule/redigo v1.8.5 github.com/hashicorp/consul/api v1.10.1 google.golang.org/grpc v1.40.0 gorm.io/driver/mysql v1.1.2 diff --git a/mysql/mysql.go b/mysql/mysql.go index 19b55b9..ec0e322 100644 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -23,10 +23,16 @@ type Config struct { } //Init mysql初始化 -func Init(conf *Config) { - if conf != nil { - _conf = conf +func Init(config *Config) { + if config == nil { + config = &Config{ + ConnString: "root:root@tcp(127.0.0.1:3306)/sample?charset=utf8&parseTime=True&loc=Local", + ConnMaxLifetime: 1, + MaxIdleConns: 10, + MaxOpenConns: 100, + } } + _conf = config } //New 创建实例 diff --git a/redis/redis.go b/redis/redis.go new file mode 100644 index 0000000..875ae48 --- /dev/null +++ b/redis/redis.go @@ -0,0 +1,129 @@ +package redis + +import ( + "encoding/json" + "time" + + redigo "github.com/gomodule/redigo/redis" +) + +var pool *redigo.Pool + +//Config 配置 +type Config struct { + Host string `yml:"host" json:"host"` + Password string `yml:"password" json:"password"` + Database int `yml:"database" json:"database"` + MaxIdle int `yml:"max_idle" json:"max_idle"` + MaxActive int `yml:"max_active" json:"max_active"` + IdleTimeout int `yml:"idle_timeout" json:"idle_timeout"` //second +} + +//Init init +func Init(opts *Config) error { + if opts == nil { + opts = &Config{ + Host: "127.0.0.1:6379", + Password: "", + Database: 0, + MaxIdle: 10, + MaxActive: 100, + IdleTimeout: 600, + } + } + + pool = &redigo.Pool{ + MaxActive: opts.MaxActive, + MaxIdle: opts.MaxIdle, + IdleTimeout: time.Second * time.Duration(opts.IdleTimeout), + Dial: func() (redigo.Conn, error) { + return redigo.Dial("tcp", opts.Host, + redigo.DialDatabase(opts.Database), + redigo.DialPassword(opts.Password), + ) + }, + TestOnBorrow: func(conn redigo.Conn, t time.Time) error { + if time.Since(t) < time.Minute { + return nil + } + _, err := conn.Do("PING") + return err + }, + } + return nil +} + +//GetBytes 获取一个字节数组值 +func GetBytes(key *string) (*[]byte, error) { + conn := pool.Get() + defer conn.Close() + + data, err := redigo.Bytes(conn.Do("GET", *key)) + return &data, err +} + +//Get 获取一个值 +func Get(key string) interface{} { + conn := pool.Get() + defer conn.Close() + + var data []byte + var err error + if data, err = redigo.Bytes(conn.Do("GET", key)); err != nil { + return nil + } + var reply interface{} + if err = json.Unmarshal(data, &reply); err != nil { + return nil + } + + return reply +} + +//Set 设置一个值 +func Set(key string, val interface{}, timeout time.Duration) (err error) { + conn := pool.Get() + defer conn.Close() + + var data []byte + if data, err = json.Marshal(val); err != nil { + return + } + + _, err = conn.Do("SETEX", key, int64(timeout/time.Second), data) + + return +} + +//IsExist 判断key是否存在 +func IsExist(key string) bool { + conn := pool.Get() + defer conn.Close() + + a, _ := conn.Do("EXISTS", key) + i := a.(int64) + return i > 0 +} + +//Delete 删除 +func Delete(key string) error { + conn := pool.Get() + defer conn.Close() + + if _, err := conn.Do("DEL", key); err != nil { + return err + } + + return nil +} + +//Expire 失效时间配置 +func Expire(key string, t int64) error { + conn := pool.Get() + defer conn.Close() + + if _, err := conn.Do("expire", key, t); err != nil { + return err + } + return nil +} diff --git a/test/redis_test.go b/test/redis_test.go new file mode 100644 index 0000000..5058ccb --- /dev/null +++ b/test/redis_test.go @@ -0,0 +1,7 @@ +package test + +import "testing" + +func TestRedis(t *testing.T) { + +} diff --git a/test/sqlite_test.go b/test/sqlite_test.go index f766438..631942b 100644 --- a/test/sqlite_test.go +++ b/test/sqlite_test.go @@ -47,7 +47,7 @@ func BenchmarkSqlite(b *testing.B) { m := rand.Intn(100) + 1 n := rand.Intn(m) for i := 0; i < n; i++ { - fmt.Sprintf("%d", i) + fmt.Printf("%d", i) } } })