relynolli-server/internal/redis.go

45 lines
1018 B
Go

package internal
import (
"context"
"log"
"os"
"strconv"
"time"
"github.com/gin-contrib/cache/persistence"
"github.com/redis/go-redis/v9"
)
var (
redisInstance *redis.Client = nil
cacheStore *persistence.RedisStore
)
type Cache interface {
}
func InitRedis() *redis.Client {
if redisInstance == nil {
redis_db_num, err := strconv.Atoi(os.Getenv("REDIS_DATABASE"))
if err != nil {
log.Fatalln("REDIS_DATABASE should be integer")
}
redisInstance = redis.NewClient(&redis.Options{Addr: os.Getenv("REDIS_ADDRESS"), Password: os.Getenv("REDIS_PASSWORD"), DB: redis_db_num, Username: os.Getenv("REDIS_USERNAME")})
_, conError := redisInstance.Ping(context.Background()).Result()
if conError != nil {
log.Fatalln("Cannot connect to redis host")
}
}
return redisInstance
}
func InitCacheStore() *persistence.RedisStore {
if cacheStore == nil {
cacheStore = persistence.NewRedisCache(os.Getenv("REDIS_ADDRESS"), os.Getenv("REDIS_PASSWORD"), 15*time.Minute)
}
return cacheStore
}