增加redis

This commit is contained in:
suguo.yao 2021-09-15 22:53:03 +08:00
parent 0603d042c5
commit 1e5ea70247
5 changed files with 147 additions and 4 deletions

1
go.mod
View File

@ -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

View File

@ -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 创建实例

129
redis/redis.go Normal file
View File

@ -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
}

7
test/redis_test.go Normal file
View File

@ -0,0 +1,7 @@
package test
import "testing"
func TestRedis(t *testing.T) {
}

View File

@ -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)
}
}
})