当前位置:网站首页>Go redis connection pool
Go redis connection pool
2022-06-30 19:12:00 【It workers】
1、 create profile
Store in conf Configure folders , It can be corresponding to your needs .
redis.go
package conf
var RedisConf = map[string]string{
"name": "redis",
"type": "tcp",
"address": "127.0.0.1:6379",
"auth": "123456",
}2、redis Connection pool
redispool.go Connection pool implementation
package redis
import (
. "example/example/conf" // Change to your own configuration directory
"github.com/garyburd/redigo/redis"
"time"
)
var RedisClient *redis.Pool
func init() {
// Set up connection pool
RedisClient = &redis.Pool{
// Get... From the configuration file maxidle as well as maxactive, If not, use the following default value
MaxIdle: 16, // Initial number of connections
// MaxActive:1000000, // Maximum number of connections
MaxActive: 0, // The maximum number of connections in the connection pool , I'm not sure I can use 0(0 Indicates automatic definition ), Distribute on demand
IdleTimeout: 300 * time.Second, // Connection closing time 300 second (300 Seconds do not use automatic shutdown )
Dial: func() (redis.Conn, error) { // To connect the redis database
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
},
}
}Examples of use :
package main
import (
"example/example/public/redispool" // Change to your own redispool.go(redis Connection pool implementation file ) The catalog of
"fmt"
"github.com/garyburd/redigo/redis"
)
var RedisExpire = 3600 // Cache lifetime
func main() {
// Get connections from the pool
rc := redispool.RedisClient.Get()
// Put the connection back into the connection pool after use
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)
// Delete
rc.Do("Del", key)
}Project address :https://github.com/guyan0319/golang_development_notes
边栏推荐
- torch stack() meshgrid()
- Evolution of screen display technology
- 服务器之间传文件夹,文件夹内容为空
- 20220607跌破建议零售价,GPU市场正全面走向供过于求...
- 音频 librosa 库 与 torchaudio 库中 的 Mel- spectrogram 进行对比
- 删除排序链表中的重复元素 II[链表节点统一操作--dummyHead]
- slice
- Compare the audio librosa library with the Mel spectrogram in the torchaudio library
- 深度学习编译器的理解
- Cloud Native Landing Practice Using rainbond for extension dimension information
猜你喜欢

3.10 haas506 2.0 development tutorial example TFT

浏览器窗口切换激活事件 visibilitychange

深度学习编译器的理解

Full recharge, im+rtc+x full communication service "feedback season" starts

How to seamlessly transition from traditional microservice framework to service grid ASM

ForkJoinPool

Swin-transformer --relative positional Bias

Digital intelligent supplier management system solution for coal industry: data driven, supplier intelligent platform helps enterprises reduce costs and increase efficiency

20220607跌破建议零售价,GPU市场正全面走向供过于求...

MRO industrial products procurement management system: enable MRO enterprise procurement nodes to build a new digital procurement system
随机推荐
Video content production and consumption innovation
华兴证券:混合云原生架构下的 Kitex 实践
At present, the big guys are joining the two streams of flinksql, cdcmysql and Kafka, and the results are put into MySQL or KA
Is it safe to open a mobile stock account? Is it reliable?
What if the apple watch fails to power on? Apple watch can not boot solution!
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
Ambient light and micro distance detection system based on stm32f1
手机股票账号开户安全吗?是靠谱的吗?
屏幕显示技术进化史
Unlimited cloud "vision" innovation | the 2022 Alibaba cloud live summit was officially launched
Four tips tell you how to use SMS to promote business sales?
torch. roll
Kalman滤波器--从高斯融合推导
Glacier teacher's book
Personally test the size of flutter after packaging APK, quite satisfied
Teach you to quickly set up a live studio in 30 minutes
Nodejs 安装与介绍
Coding officially entered Tencent conference application market!
「干货」数据分析常用的10种统计学方法,附上重点应用场景
金融服务行业SaaS项目管理系统解决方案,助力企业挖掘更广阔的增长服务空间