Golang uses Redis with connection pool
Use the redis package to download go get /gomodule/redigo/redis if the network is not good, it will be very difficult.
package main import ( "fmt" "/gomodule/redigo/redis" // Introduce redis package) func main() { //Connect the data source rediss, err := ("tcp", "127.0.01:6379") if err != nil { ("Connection exception", err) } //Insert string data test, err := ("set", "test", "hi") if err != nil { ("Insert data failed", err) } (test) //Read string data str, err := (("get", "test")) (str) //hash type do, _ := ("hset", "hh", "name", "zhangsn") (do) hh, _ := (("hget", "hh", "name")) (hh) //Set the key expiration time ("expire", "hh", 1) //Close redis () }
Redis Data Source Connection Pool
package main import ( "fmt" "/gomodule/redigo/redis" // Introduce redis package) var pool * func init() { pool = &{ MaxIdle: 8, //Maximum number of idle connections MaxActive: 0, //Indicate the maximum number of connections to the database. 0 means no limit IdleTimeout: 100, //Maximum free time Dial: func() (, error) { //Initialize the connection redis address return ("tcp", "127.0.01:6379") }, } } func main() { //Get the connection conn := () //Insert data do, err := ("set", "11", "11") if err != nil { ("Insert failed", err) } (do) //Close redis () }
Golang Redis connection pool encapsulation
Create a connection pool method file
package dao import ( "fmt" "/gomodule/redigo/redis" "/ini.v1" "os" "sync" "time" ) var once // RedisClient Redis Servicetype RedisClient struct { Client * } //Redis Global Redisvar RedisPool *RedisClient //ConnectRedis Connect to the redis database and set the global Redis objectfunc ConnectRedis() { config, err := ("./config/") if err != nil { //fail ("Fail to read file: %v", err) (1) } address := ("redis").Key("address").String() password := ("redis").Key("password").String() db, _ := ("redis").Key("db").Int() (func() { RedisPool = NewClient(address, password, db) }) con_err := () if con_err != nil { panic(con_err) } } // NewClient creates a new redis connectionfunc NewClient(address string, password string, db int) *RedisClient { // Initialize the custom RedisClient instance rds := &RedisClient{} // Initialize the connection using redis Curry's NewClient = &{ MaxIdle: 100, //Maximum free MaxActive: 1000, //Maximum connection IdleTimeout: (60) * , Wait: true, Dial: func() (, error) { c, err := ( "tcp", address, (password), (int(db)), ((60)*), ((60)*), ((60)*), ) if err != nil { return nil, err } return c, err }, } return rds } // Ping is used to test whether the redis connection is normalfunc (rds *RedisClient) Ping() error { _, err := ().Do("ping") return err } // Set stores the value corresponding to the key and sets the expiration expiration time (units nanoseconds)func (rds *RedisClient) Setex(key string, expiration int, value interface{}) bool { conn := () defer () if _, err := ("setex", key, expiration, value); err != nil { (err) return false } return true } // //Get get the value corresponding to the keyfunc (rds *RedisClient) Get(key string) string { conn := () defer () result, err := (("Get", key)) if err != nil { return "" } return result } //Get get the value corresponding to the keyfunc (rds *RedisClient) Rpop(key string) (string, error) { conn := () defer () result, err := (("Rpop", key)) if err != nil { return "", err } return result, nil }
Configuration File
app_name = go-gin [mysql] ip = 127.0.0.1 port = 3306 user = root password = root database = test prefix = tt_ #Maximum number of connectionsMaxIdleConns = 500 #Maximum free timeMaxOpenConns = 50 [redis] address = 127.0.0.1:6379 password = 123456 db = 7
Calling methods
func main() { () //Initialize the connection redis defer () //Execute close before exit res, err := ("aaa") if err != nil { (err) } (res) }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.