package redis import ( "context" "matchmaking-system/internal/conf" "matchmaking-system/internal/pkg/flags" "time" red "github.com/redis/go-redis/v9" ) // NewRedis // // @Description: // @param c // @return *red.Client func NewRedis(c *conf.Data) *red.Client { client := red.NewClient(&red.Options{ Addr: c.Redis.Addr, DB: int(c.Redis.Db), Password: c.Redis.Password, // no password set }) _, err := client.Ping(context.Background()).Result() if err != nil { panic(err) } return client } // GetCacheData // // @Description: Query data through key // @param ctx // @param redisDB // @param key // @return string // @return error func GetCacheData(ctx context.Context, redisDB *red.Client, key string) (string, error) { rge, err := redisDB.Get(ctx, key).Result() if err != nil { return flags.SetNull, err } return rge, nil } // SetCacheData // // @Description: // @param ctx // @param redisDB // @param key // @param value // @param td // @return error func SetCacheData(ctx context.Context, redisDB *red.Client, key string, value interface{}, td int) error { if err := redisDB.Set(ctx, key, value, time.Duration(td)*time.Minute).Err(); err != nil { return err } return nil } // GetCacheKeys // // @Description: Query all keys // @param ctx // @param redisDB // @return []string // @return error func GetCacheKeys(ctx context.Context, redisDB *red.Client) ([]string, error) { rge, err := redisDB.Keys(ctx, "*").Result() if err != nil { return []string{}, err } return rge, nil } // GetCacheCount // // @Description: Query the total number of Redis // @param ctx // @param redisDB // @param key // @return int64 // @return error func GetCacheCount(ctx context.Context, redisDB *red.Client, key string) (int64, error) { rge, err := redisDB.DBSize(ctx).Result() if err != nil { return rge, err } return rge, nil } // SetCacheValue // // @Description: persistent data // @param ctx // @param redisDB // @param key // @param value // @return error func SetCacheValue(ctx context.Context, redisDB *red.Client, key, value string) error { if err := redisDB.Set(ctx, key, value, 0).Err(); err != nil { return err } return nil }