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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐