当前位置:网站首页>Goframe gredis configuration management | comparison of configuration files and configuration methods
Goframe gredis configuration management | comparison of configuration files and configuration methods
2022-06-12 17:32:00 【Wangzhongyang go】
Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 18 God , Click to see the event details
Preface abstract
The last article introduced GoFrame gcache Use practice | Cache control Elimination strategy , Got positive feedback from everyone .
The next few articles have made persistent efforts , Still introduce to you GoFrame Framework cache related knowledge points , And some summary thoughts in the use of my project ,GoFrame The framework is hereinafter referred to as gf.
GoFrame gredis
goframe The framework supports two ways to manage redis Configure and get redis object , One is through the configuration file + Singleton objects ; One is the modular configuration management method and object creation method .
We use the former : Through the configuration file + Singleton objects .
The configuration file ( recommend )
In most cases, it is recommended to use g.Redis Single instance operation redis.
It is recommended to use the configuration file to manage Redis To configure , stay config.toml The configuration example in is as follows :
# Redis Database configuration
[redis]
default = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1,123456?idleTimeout=600"
Copy code among ,Redis The configuration format of is :
host:port[,db,pass?maxIdle=x&maxActive=x&idleTimeout=x&maxConnLifetime=x]
The configuration items are described as follows :
| Configuration item name | Whether must | The default value is | explain |
|---|---|---|---|
host | yes | - | Address |
port | yes | - | port |
db | no | 0 | database |
pass | no | - | Authorization code |
maxIdle | no | 10 | Number of idle connections allowed (0 Means unrestricted ) |
maxActive | no | 100 | The maximum number of connections is limited (0 Means unrestricted ) |
idleTimeout | no | 10 | Maximum connection idle time ( Unit second , Setting to... Is not allowed 0) |
maxConnLifetime | no | 30 | Maximum connection lifetime ( Unit second , Setting to... Is not allowed 0) |
tls | no | false | Whether to use TLS authentication |
skipVerify | no | false | adopt TLS When the connection , Whether to disable server name validation |
Among them default and cache Respectively represents the name of the configuration group , In the program, we can get the corresponding configuration through this name redis Singleton object .
When the group name is not passed , By default redis.default Configure grouped items ) To get the corresponding configuration redis Client singleton object . After execution , The output is :
Examples of use :
package main
import (
"fmt"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/util/gconv"
)
func main() {
g.Redis().DoVar("SET", "k", "v")
v, _ := g.Redis().DoVar("GET", "k")
fmt.Println(v.String())
}
Copy code Print the results
v
Copy code Configuration method ( senior )
because gf Is a modular framework , In addition to the coupling and convenience g Module to automatically parse configuration files and obtain singleton objects , It also supports developers' modular use gredis package .
But this usage is relatively demanding for developers .
gredis It provides global grouping configuration function , The relevant configuration management methods are as follows :
func SetConfig(config Config, name ...string)
func GetConfig(name ...string) (config Config, ok bool)
func RemoveConfig(name ...string)
func ClearConfig()
Copy code among name The parameter is the group name , That is, to manage configuration objects by grouping , We can set different group names for different configuration objects , Then we can pass Instance Singleton method acquisition redis Client operation object singleton .
func Instance(name ...string) *Redis
Copy code Examples of use :
package main
import (
"fmt"
"github.com/gogf/gf/database/gredis"
"github.com/gogf/gf/util/gconv"
)
var (
config = gredis.Config{
Host : "127.0.0.1",
Port : 6379,
Db : 0,
}
)
func main() {
group := "test"
gredis.SetConfig(&config, group)
redis := gredis.Instance(group)
defer redis.Close()
_, err := redis.Do("SET", "k", "v")
if err != nil {
panic(err)
}
r, err := redis.Do("GET", "k")
if err != nil {
panic(err)
}
fmt.Println(gconv.String(r))
}
Copy code summary
gredis There are many relevant knowledge points , So I plan to split it into several articles for updating .
This article mainly introduces gredis Configuration mode : Support configuration file management , It also supports management through configuration methods .
We use the officially recommended configuration file management method : The method of configuration file management also supports sub databases .
Last
Thank you for reading , Welcome to the third company : give the thumbs-up 、 Collection 、 Coin-operated ( Focus on )!!!
边栏推荐
- The R language uses the aggregate The plot function visualizes the summary statistical information of each subset (visualization is based on the probability value and its 95% confidence interval of th
- 2080 virtual machine login command
- Li Kou today's question 926 Flip string to monotonic increment
- 力扣今日题926. 将字符串翻转到单调递增
- Alibaba cloud image station supports IPv6!
- 布局管理中的sizePolicy的策略问题
- 淘宝Native研发模式的演进与思考 | DX研发模式
- 《用户体验要素:以用户为中心的产品设计》笔记
- Arm64栈回溯
- 全局锁、表锁、行锁
猜你喜欢

记录使用yolov5进行旋转目标的检测

Qiushengchang: Practice of oppo commercial data system construction

Gerrit triggers Jenkins sonarqube scan

Evolution and thinking of Taobao native R & D mode | DX R & D mode

借助SpotBugs将程序错误扼杀在摇篮中

进阶之大山-asp.net core 路由程序基础使用演示0.1

Arm64棧回溯

How to win the "Olympic Games" in retail technology for jd.com, the learning tyrant of the "regular examination"?

C#操作数据库增查业务数据值内容案例学校表

淘宝Native研发模式的演进与思考 | DX研发模式
随机推荐
Figma from getting started to giving up
The R language uses the tabpct function of the epidisplay package to generate a two-dimensional contingency table, and uses the mosaic diagram to visualize the contingency table (two-dimensional conti
消息队列实战之队列优先级
How to use the official documents of pytorch and torchvision
Li Kou today's question 926 Flip string to monotonic increment
The R language uses the plot function to visualize the data scatter chart, and uses font The axis parameter specifies that the font type of the axis scale label is italic
selenium元素定位
MySQL authorization summary
[CSP]202012-2期末预测之最佳阈值
价值超高的CA证书解析
ssm常用到的依赖
写技术博客的意义
Evolution and thinking of Taobao native R & D mode | DX R & D mode
R language calculates data Table specifies the mean value of a numeric variable when the value of one grouped variable is fixed and another grouped variable
Tidb Hackathon 2021 - pcloud: conduct icloud pcloud team interview on the database
Learn the mitmproxy packet capturing tool from scratch
全局锁、表锁、行锁
Analysis of CA certificate with high value
2080 virtual machine login command
错误记录:IllegalStateException: Optional int parameter ‘xxxx‘ is