当前位置:网站首页>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
边栏推荐
- The easynvr platform equipment channels are all online. What is the reason for the "network request failure" in the operation?
- 拓维信息使用 Rainbond 的云原生落地实践
- MySQL transaction concurrency and mvcc mechanism
- 熵-条件熵-联合熵-互信息-交叉熵
- torch stack() meshgrid()
- How does rust implement dependency injection?
- com.alibaba.fastjson.JSONObject # toJSONString 消除循环引用
- mysql修改数据类型_MySQL修改字段类型[通俗易懂]
- mysql for update 死锁问题排查
- 亲测flutter打包apk后大小,比较满意
猜你喜欢

Opencv data type code table dtype

sqlserver SQL Server Management Studio和Transact-SQL创建账户、创建访问指定数据库的只读用户

Coding officially entered Tencent conference application market!

详解单例模式

AI chief architect 10-aica-lanxiang, propeller frame design and core technology

屏幕显示技术进化史

ForkJoinPool

Deep learning compiler understanding

Lenovo Yoga 27 2022, full upgrade of super configuration
Do you really understand the persistence mechanism of redis?
随机推荐
「经验」爬虫在工作中的实战应用『理论篇』
How does rust implement dependency injection?
GameFi链游系统开发NFT技术
拓维信息使用 Rainbond 的云原生落地实践
德国AgBB VoC有害物质测试
《客从何处来》
TCP packet sticking problem
Where do the guests come from
German agbb VOC hazardous substances test
MySQL事务并发问题和MVCC机制
Personally test the size of flutter after packaging APK, quite satisfied
[Collection - industry solutions] how to build a high-performance data acceleration and data editing platform
NEON优化2:ARM优化高频指令总结
Construction and practice of full stack code test coverage and use case discovery system
Swin-Transformer(2021-08)
Swin-Transformer(2021-08)
屏幕显示技术进化史
torch stack() meshgrid()
Countdowncatch and completabilefuture and cyclicbarrier
sqlserver SQL Server Management Studio和Transact-SQL创建账户、创建访问指定数据库的只读用户