himbot/lib/redis.go

55 lines
1 KiB
Go
Raw Normal View History

2023-12-27 12:47:14 -07:00
package lib
import (
"context"
"os"
"time"
"github.com/redis/go-redis/v9"
)
func SetCache(key string, value string, ttlMinutes int) bool {
var redis_host = os.Getenv("REDIS_HOST")
var redis_password = os.Getenv("REDIS_PASSWORD")
2023-12-27 12:47:14 -07:00
rdb := redis.NewClient(&redis.Options{
Addr: redis_host,
Password: redis_password,
DB: 0,
})
if rdb == nil {
panic("Failed to create Redis client")
}
2023-12-27 12:47:14 -07:00
println("Created Client in Set")
2023-12-27 12:48:47 -07:00
err := rdb.Set(context.Background(), key, value, time.Minute*time.Duration(ttlMinutes)).Err()
if err != nil {
panic(err)
}
2023-12-27 12:47:14 -07:00
return err != nil
}
func GetCache(key string) string {
var redis_host = os.Getenv("REDIS_HOST")
var redis_password = os.Getenv("REDIS_PASSWORD")
println("Entered Get")
2023-12-27 12:47:14 -07:00
rdb := redis.NewClient(&redis.Options{
Addr: redis_host,
Password: redis_password,
DB: 0,
})
if rdb == nil {
panic("Failed to create Redis client")
}
2023-12-27 12:47:14 -07:00
val, err := rdb.Get(context.Background(), key).Result()
2023-12-27 12:47:14 -07:00
if err != nil {
return "nil"
}
println("Called Get")
2023-12-27 12:47:14 -07:00
return val
}