当前位置:网站首页>Go Redis连接池
Go Redis连接池
2022-06-30 17:46:00 【IT工作者】
1、创建配置文件
存放在conf配置文件夹,可以跟你的需要存在相应。
redis.go
package conf
var RedisConf = map[string]string{
"name": "redis",
"type": "tcp",
"address": "127.0.0.1:6379",
"auth": "123456",
}2、redis连接池
redispool.go 连接池实现
package redis
import (
. "example/example/conf" //改成你自己配置目录
"github.com/garyburd/redigo/redis"
"time"
)
var RedisClient *redis.Pool
func init() {
// 建立连接池
RedisClient = &redis.Pool{
// 从配置文件获取maxidle以及maxactive,取不到则用后面的默认值
MaxIdle: 16, //最初的连接数量
// MaxActive:1000000, //最大连接数量
MaxActive: 0, //连接池最大连接数量,不确定可以用0(0表示自动定义),按需分配
IdleTimeout: 300 * time.Second, //连接关闭时间 300秒 (300秒不使用自动关闭)
Dial: func() (redis.Conn, error) { //要连接的redis数据库
c, err := redis.Dial(RedisConf["type"], RedisConf["address"])
if err != nil {
return nil, er
}
if _, err := c.Do("AUTH", RedisConf["auth"]); err != nil {
c.Close()
return nil, er
}
return c, nil
},
}
}使用示例:
package main
import (
"example/example/public/redispool" //改成你自己的redispool.go(redis连接池实现文件)的目录
"fmt"
"github.com/garyburd/redigo/redis"
)
var RedisExpire = 3600 //缓存有效期
func main() {
// 从池里获取连接
rc := redispool.RedisClient.Get()
// 用完后将连接放回连接池
defer rc.Close()
key := "redis.cache"
_, err := rc.Do("Set", key, "1", "EX", RedisExpire)
if err != nil {
fmt.Println(err)
return
}
val, err := redis.String(rc.Do("Get", key))
if err != nil {
fmt.Println(err)
}
fmt.Println(val)
//删除
rc.Do("Del", key)
}项目地址:https://github.com/guyan0319/golang_development_notes
边栏推荐
- MySQL找不到mysql.sock文件的临时解
- Entry node of link in linked list - linked list topic
- Redis - persistent RDB and persistent AOF
- Memory Limit Exceeded
- Tensorflow2 ten must know for deep learning
- Teach you to quickly set up a live studio in 30 minutes
- 深度学习编译器的理解
- Can go struct in go question bank · 15 be compared?
- Helping the ultimate experience, best practice of volcano engine edge computing
- MRO工业品采购管理系统:赋能MRO企业采购各节点,构建数字化采购新体系
猜你喜欢

Dlib库实现人脸关键点检测(Opencv实现)

4个技巧告诉你,如何使用SMS促进业务销售?

链表中环的入口结点-链表专题

Classic problem of leetcode dynamic programming (I)

CODING 正式入驻腾讯会议应用市场!

Reading notes of "high EQ means being able to talk"

传统微服务框架如何无缝过渡到服务网格 ASM
Do you really understand the persistence mechanism of redis?

PC wechat multi open

Helping the ultimate experience, best practice of volcano engine edge computing
随机推荐
PC端微信多开
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(上)
Vulnerability recurrence ----- 35. Uwsgi PHP directory traversal vulnerability (cve-2018-7490)
How to do a good job in software system demand research? Seven weapons make it easy for you to do it
《客从何处来》
Classic problem of leetcode dynamic programming (I)
Detailed single case mode
MySQL cannot find mysql Temporary solution of sock file
Rust 书籍资料 - 芽之家书馆
Tensorflow2 深度学习十必知
Helping the ultimate experience, best practice of volcano engine edge computing
如何利用AI技术优化独立站客服系统?听听专家怎么说!
领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
Redis - persistent RDB and persistent AOF
Dependencies tool to view exe and DLL dependencies
《Go题库·15》go struct 能不能比较?
电子元器件招标采购商城:优化传统采购业务,提速企业数字化升级
The cloud native landing practice of using rainbow for Tuowei information
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
基于STM32F1的环境光与微距离检测系统