Test library github.com/gomodule/redigo/redis

This commit is contained in:
MathieuHa
2022-10-17 20:58:27 +02:00
parent 87ed9e9c4e
commit 4058836678
77 changed files with 4176 additions and 17306 deletions
+25 -22
View File
@@ -3,9 +3,7 @@ package cache
import (
"context"
"fmt"
"time"
"github.com/go-redis/redis/v9"
ttl_map "github.com/leprosus/golang-ttl-map"
logger "github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/pkg/logger"
@@ -18,7 +16,7 @@ const (
var ctx = context.Background()
var cache = ttl_map.New()
var rdb *redis.Client
var redisEnabled = false
// Get Decision check in the cache if the IP has the banned / not banned value.
@@ -41,12 +39,13 @@ func getDecisionLocalCache(clientIP string) (bool, error) {
}
func getDecisionRedisCache(clientIP string) (bool, error) {
banned, err := rdb.Get(ctx, clientIP).Result()
if err != nil {
return false, fmt.Errorf("no cache data")
} else {
return banned == cacheBannedValue, nil
}
// banned, err := rdb.Get(ctx, clientIP).Result()
// if err != nil {
// return false, fmt.Errorf("no cache data")
// } else {
// return banned == cacheBannedValue, nil
// }
return false, nil
}
func SetDecision(clientIP string, isBanned bool, duration int64) {
@@ -65,10 +64,10 @@ func SetDecision(clientIP string, isBanned bool, duration int64) {
}
func SetDecisionRedisCache(clientIP string, value string, duration int64) {
err := rdb.Set(ctx, clientIP, value, time.Duration(duration)).Err()
if err != nil {
logger.Info("Error, could not insert in redis cache IP")
}
// err := rdb.Set(ctx, clientIP, value, time.Duration(duration)).Err()
// if err != nil {
// logger.Info("Error, could not insert in redis cache IP")
// }
// errors are not handled
}
@@ -86,10 +85,10 @@ func DeleteDecision(clientIP string) {
}
func DeleteDecisionRedisCache(clientIP string) {
err := rdb.Del(ctx, clientIP).Err()
if err != nil {
logger.Info("Error, could not delete in redis cache IP")
}
// err := rdb.Del(ctx, clientIP).Err()
// if err != nil {
// logger.Info("Error, could not delete in redis cache IP")
// }
// errors are not handled
}
@@ -98,10 +97,14 @@ func DeleteDecisionLocalCache(clientIP string) {
}
func InitRedisClient(addr string, port int, password string) {
rdb = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%v:%d", addr, port),
Password: password,
DB: 0, // use default DB
})
// c, _ := redis.DialContext(ctx, "tcp", ":6379")
// defer c.Close()
// c, err := redis.DialURL(fmt.Sprintf("%v:%d", addr, port))
// if err != nil && c != nil{
// // handle error
// // c.Do("Set", "key", "value")
// logger.Debug("test")
// }
// TODO defer c.Close()
redisEnabled = true
}