diff --git a/pkg/scalers/redis_scaler.go b/pkg/scalers/redis_scaler.go index bcbb7c4c375..1c88f29bfcb 100644 --- a/pkg/scalers/redis_scaler.go +++ b/pkg/scalers/redis_scaler.go @@ -197,9 +197,30 @@ func getRedisListLength(ctx context.Context, address string, password string, li } client := redis.NewClient(options) + var listType *redis.StatusCmd - cmd := client.LLen(listName) + listType = client.Type(listName) + if listType.Err() != nil { + return -1, listType.Err() + } + + var cmd *redis.IntCmd + switch listType.Val() { + case "list": + cmd = client.LLen(listName) + case "set": + cmd = client.SCard(listName) + case "hash": + cmd = client.HLen(listName) + case "zset": + cmd = client.ZCard(listName) + default: + cmd = nil + } + if cmd == nil { + return -1, fmt.Errorf("list must be of type:list,set,hash,zset but was %s", listType.Val()) + } if cmd.Err() != nil { return -1, cmd.Err() }