MySql教程

GIN试玩:GORM连接MYSQL,GIN连接REDIS

本文主要是介绍GIN试玩:GORM连接MYSQL,GIN连接REDIS,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Mysql

安装依赖

go get "github.com/jinzhu/gorm"
go get "github.com/jinzhu/gorm/dialects/mysql"

主体内容在init函数中,main.go直接引入即可

// mysql.go
package Mysql

import (
	"fmt"
	"github.com/jinzhu/gorm"
	"go-service/src/Config"
	"strings"
)
import _ "github.com/jinzhu/gorm/dialects/mysql"

var DB *gorm.DB

func init() {
	path := strings.Join([]string{Config.MYSQL_URL.UserName, ":", Config.MYSQL_URL.Password, "@tcp(", Config.MYSQL_URL.Ip, ":", Config.MYSQL_URL.Port, ")/", Config.MYSQL_URL.DataBase, "?charset=utf8"}, "")
	var err error
	DB, err = gorm.Open("mysql", path)
	if err != nil {
		DB.Close()
		fmt.Println(err.Error())
	}
	fmt.Println("已连接")
	// 设置连接池,空闲连接
	DB.DB().SetMaxIdleConns(50)
	// 打开链接
	DB.DB().SetMaxOpenConns(100)

	// 表明禁用后缀加s
	DB.SingularTable(true)
}

Redis

安装依赖

go get "github.com/go-redis/redis/v8"

与mysql同步

// redis.go
package Redis

import (
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	"go-service/src/Config"
	"time"
)

var (
	Redis *redis.Client
)

var ctx = context.Background()

func init() {
	Redis = redis.NewClient(&redis.Options{
		Addr:     Config.REDIS_URL.Ip + ":" + Config.REDIS_URL.Port,
		Password: "", // no password set
		DB:       0,  // use default DB
	})
}

func SetRedis(key string, value string, t int) bool {
	expire := time.Duration(t) * time.Second
	if err := Redis.Set(ctx, key, value, expire).Err(); err != nil {
		fmt.Println(err)
		return false
	}
	return true
}

func GetRedis(key string) string {
	result, err := Redis.Get(ctx, key).Result()
	if err != nil {
		fmt.Println(err)
		return ""
	}
	return result
}

func DelRedis(key string) bool {
	_, err := Redis.Del(ctx, key).Result()
	if err != nil {
		fmt.Println(err)
		return false
	}
	return true
}
// 延长过期时间
func ExpireRedis(key string, t int) bool {
	expire := time.Duration(t) * time.Second
	if err := Redis.Expire(ctx, key, expire).Err(); err != nil {
		fmt.Println(err)
		return false
	}
	return true
}

运行

package main

import (
	_ "go-service/src/Mysql"
	_ "go-service/src/Redis"
)
这篇关于GIN试玩:GORM连接MYSQL,GIN连接REDIS的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!