当前位置:网站首页>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
边栏推荐
- Distributed transaction
- MySQL transaction concurrency and mvcc mechanism
- Sword finger offer 17 Print from 1 to maximum n digits
- 《客从何处来》
- 云上“视界” 创新无限 | 2022阿里云直播峰会正式上线
- When selecting smart speakers, do you prefer "smart" or "sound quality"? This article gives you the answer
- Compare the audio librosa library with the Mel spectrogram in the torchaudio library
- 电子元器件行业在线采购系统精准匹配采购需求,撬动电子产业数字化发展
- TCP packet sticking problem
- C WinForm program interface optimization example
猜你喜欢

屏幕显示技术进化史

Swin-Transformer(2021-08)

医院在线问诊小程序源码 互联网医院源码 智慧医院源码

What if the apple watch fails to power on? Apple watch can not boot solution!

The easynvr platform equipment channels are all online. What is the reason for the "network request failure" in the operation?

opencv数据类型代码表 dtype

PC端微信多开

ONEFLOW source code parsing: automatic inference of operator signature

Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills

Cloud Native Landing Practice Using rainbond for extension dimension information
随机推荐
OneFlow源码解析:算子签名的自动推断
Vulnerability recurrence ----37. Apache unomi Remote Code Execution Vulnerability (cve-2020-13942)
详解单例模式
Rust 文件系统处理之文件读写 - Rust 实践指南
Ambient light and micro distance detection system based on stm32f1
Do you really understand the persistence mechanism of redis?
Multipass中文文档-设置图形界面
Pytorch learning (III)
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(上)
ForkJoinPool
How to use AI technology to optimize the independent station customer service system? Listen to the experts!
电子元器件行业在线采购系统精准匹配采购需求,撬动电子产业数字化发展
Is it safe to open an account for goucai? Is it reliable?
Tensorflow2 深度学习十必知
Is it safe to open a mobile stock account? Is it reliable?
挖财账号开户安全吗?是靠谱的吗?
删除排序链表中的重复元素 II[链表节点统一操作--dummyHead]
ForkJoinPool
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
[community star selection] the 23rd issue of the July revision plan | bit by bit creation, converging into a tower! Huawei freebuses 4E and other cool gifts