当前位置:网站首页>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
边栏推荐
- Sword finger offer 16 Integer power of numeric value
- Distributed transaction
- 《Go题库·15》go struct 能不能比较?
- Volcano engine was selected into the first "panorama of edge computing industry" in China
- Reading notes of "high EQ means being able to talk"
- Rust 如何实现依赖注入?
- Infineon - GTM architecture -generic timer module
- How to do a good job in software system demand research? Seven weapons make it easy for you to do it
- 服务器之间传文件夹,文件夹内容为空
- Detailed single case mode
猜你喜欢

Countdowncatch and completabilefuture and cyclicbarrier

Cloud Native Landing Practice Using rainbond for extension dimension information

Troubleshooting MySQL for update deadlock

How to use AI technology to optimize the independent station customer service system? Listen to the experts!

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

Vulnerability recurrence ----- 38. Thinkphp5 5.0.23 Remote Code Execution Vulnerability

煤炭行业数智化供应商管理系统解决方案:数据驱动,供应商智慧平台助力企业降本增效

Multipass Chinese document - setting graphical interface

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

正则表达式(正则匹配)
随机推荐
电子元器件行业在线采购系统精准匹配采购需求,撬动电子产业数字化发展
链表中环的入口结点-链表专题
挖财账号开户安全吗?是靠谱的吗?
Rust 操控大疆可编程无人机 tello
Can go struct in go question bank · 15 be compared?
Do you really understand the persistence mechanism of redis?
《Go题库·15》go struct 能不能比较?
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
dtd建模
一点比较有意思的模块
The easynvr platform equipment channels are all online. What is the reason for the "network request failure" in the operation?
ForkJoinPool
Hospital online consultation applet source code Internet hospital source code smart hospital source code
麻烦问下 Flink支持同步数据到 sqlserver么
Memory Limit Exceeded
《客从何处来》
Swin-Transformer(2021-08)
LeetCode动态规划经典题(一)
torch.roll
Rust 文件系统处理之文件读写 - Rust 实践指南