当前位置:网站首页>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
边栏推荐
- 【TiDB】TiCDC canal_json的实际应用
- mysql for update 死锁问题排查
- 拓维信息使用 Rainbond 的云原生落地实践
- 3.10 haas506 2.0开发教程-example-TFT
- Swin-Transformer(2021-08)
- Courage to be hated: Adler's philosophy class: the father of self inspiration
- 不同制造工艺对PCB上的焊盘的影响和要求
- 拓維信息使用 Rainbond 的雲原生落地實踐
- Swin-transformer --relative positional Bias
- dtd建模
猜你喜欢

Classic problem of leetcode dynamic programming (I)

Adhering to the concept of 'home in China', 2022 BMW children's traffic safety training camp was launched

3.10 haas506 2.0开发教程-example-TFT

Unlimited cloud "vision" innovation | the 2022 Alibaba cloud live summit was officially launched

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

Multipass Chinese document - setting graphical interface

Video content production and consumption innovation

Opencv data type code table dtype

Evolution of screen display technology

深度学习编译器的理解
随机推荐
go之web框架 iris
Solution of enterprise supply chain system in medical industry: realize collaborative visualization of medical digital intelligent supply chain
The folder is transferred between servers. The folder content is empty
Opencv data type code table dtype
Rust 如何实现依赖注入?
开发那些事儿:如何在视频中添加文字水印?
How to seamlessly transition from traditional microservice framework to service grid ASM
Ambient light and micro distance detection system based on stm32f1
When selecting smart speakers, do you prefer "smart" or "sound quality"? This article gives you the answer
Is it safe to open a mobile stock account? Is it reliable?
基于 actix、async-graphql、rbatis、pgsql/mysql 构建 GraphQL 服务(4)-变更服务
金融服务行业SaaS项目管理系统解决方案,助力企业挖掘更广阔的增长服务空间
手机股票账号开户安全吗?是靠谱的吗?
Small program container technology to promote the operation efficiency of the park
Teach you to quickly set up a live studio in 30 minutes
拓維信息使用 Rainbond 的雲原生落地實踐
法国A+ 法国VOC标签最高环保级别
German agbb VOC hazardous substances test
openGauss数据库源码解析系列文章—— 密态等值查询技术详解(上)
华兴证券:混合云原生架构下的 Kitex 实践