Hello friends! In this post, I will be discussing about how we can create Redis Pool in Golang using redigo module. First of all, create a directory redis-pool and open it your favourite browser. I am using vs code. Now run below command to initialise the module:

go mod init redis-pool

Now, create a file main.go.


The whole code will look like below:

package main

import (
"log"
"github.com/gomodule/redigo/redis"
)

var redisPool *redis.Pool

func init() {
redisPool = &redis.Pool{
Dial: func() (redis.Conn, error) {
conn, err := redis.Dial("tcp", "localhost:6379")
if err != nil {
log.Fatal("Oops some error: ", err.Error())
}

return conn, err
},

MaxIdle: 10,
MaxActive: 10,
}
}

func setString(key, value string) {
con := redisPool.Get()
defer con.Close()

result, err := con.Do("SET", key, value)
if err != nil {
log.Fatal("Some error: ", err.Error())
}

log.Println(result)
}

func main() {
setString("Name", "Debug Boss")
}