Golang Gin框架搭建项目(三)使用redis
Golang Gin框架搭建项目(三)使用redis。go get -v github.com/gomodule/redigo/redis
·
1、安装redis库 go get -v github.com/gomodule/redigo/redis 2、帮助类:RedisUtil
package utils
import (
"github.com/gomodule/redigo/redis"
"strconv"
)
type RedisUtil struct {
client redis.Conn
}
//全局变量, 外部使用utils.RedisClient来访问
var RedisClient RedisUtil
//初始化redis
func InitRedisUtil(address string, port int, pwd string) (*RedisUtil, error) {
//连接redis
client, err := redis.Dial("tcp", address+":"+strconv.Itoa(port))
if err != nil {
panic("failed to redis:" + err.Error())
}
//验证redis redis的配置文件redis.conf中一定要设置quirepass=password, 不然连不上
_, err = client.Do("auth", pwd)
if err != nil {
panic("failed to auth redis:" + err.Error())
}
//初始化全局redis结构体
RedisClient = RedisUtil{client: client}
return &RedisClient, nil
}
//设置数据到redis中(string)
func (rs *RedisUtil) SetStr(key string, value string) error {
_, err := rs.client.Do("Set", key, value)
return err
}
//设置数据到redis中(string)
func (rs *RedisUtil) SetStrNotExist(key string, value string, expireSecond int) bool {
val, err := rs.client.Do("SET", key, value, "EX", expireSecond, "NX")
if err != nil || val == nil {
return false
}
return true
}
//设置数据到redis中(string)
func (rs *RedisUtil) SetStrWithExpire(key string, value string, expireSecond int) error {
_, err := rs.client.Do("Set", key, value, "ex", expireSecond)
return err
}
//获取redis中数据(string)
func (rs *RedisUtil) GetStr(key string) (string, error) {
val, err := rs.client.Do("Get", key)
if err != nil {
return "", err
}
return string(val.([]byte)), nil
}
//设置数据到redis中(hash)
func (rs *RedisUtil) HSet(key string, field string, value string) error {
_, err := rs.client.Do("HSet", key, field, value)
return err
}
//设置数据到redis中(hash)
func (rs *RedisUtil) HGet(key string, field string) (string, error) {
val, err := rs.client.Do("HGet", key, field)
if err != nil {
return "", err
}
return string(val.([]byte)), nil
}
//删除
func (rs *RedisUtil) DelByKey(key string) error {
_, err := rs.client.Do("DEL", key)
return err
}
//设置key过期时间
func (rs *RedisUtil) SetExpire(key string, expireSecond int) error {
_, err := rs.client.Do("EXPIRE", key, expireSecond)
return err
}
3、main.go配置和初始化redis:
package main
import (
"gin_demo/config"
"gin_demo/routers"
"gin_demo/utils"
"github.com/gin-gonic/gin"
)
func main() {
conf, err := config.ParseConfig("./config/app.json")
if err != nil {
panic("读取配置文件失败," + err.Error())
}
//fmt.Printf("conf:%#v\n", conf)
utils.InitRedisUtil(conf.RedisConfig.Addr, conf.RedisConfig.Port, conf.RedisConfig.Password)
//创建一个默认的路由引擎
engine := gin.Default()
routers.RegisterRouter(engine)
engine.Run(":9090")
}
4、在接口中使用redis
type UserController struct {
}
//查询用户
func (controller *UserController) Get(context *gin.Context) {
id := context.Query("id")
utils.RedisClient.SetStr("hello", "111")
utils.RedisClient.SetStrWithExpire("hello22", "22", 100)
hello := utils.RedisClient.GetStr("hello")
context.JSON(http.StatusOK, gin.H{
"id": id,
"conf": config.GetConfig(),
"hello": hello,
})
}
更多推荐
已为社区贡献5条内容
所有评论(0)