From 76a9c7f34b59559c964f098f6adac048249ddf71 Mon Sep 17 00:00:00 2001 From: ssongliu Date: Tue, 7 Mar 2023 11:18:06 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=20redis=20=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=BF=AE=E6=94=B9=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/database_redis.go | 62 +++++++++++++-------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/backend/app/service/database_redis.go b/backend/app/service/database_redis.go index 8e45fc92b..f51abc660 100644 --- a/backend/app/service/database_redis.go +++ b/backend/app/service/database_redis.go @@ -221,41 +221,41 @@ func confSet(redisName string, changeConf []redisConfig) error { } files := strings.Split(string(lineBytes), "\n") - isStartRange := false - isEndRange := false + startIndex, endIndex := 0, 0 var newFiles []string - for _, line := range files { - if !isStartRange { - if line == "# Redis configuration rewrite by 1Panel" { - isStartRange = true - } - newFiles = append(newFiles, line) - continue + for i := 0; i < len(files); i++ { + if files[i] == "# Redis configuration rewrite by 1Panel" { + startIndex = i } - if !isEndRange { - isExist := false - for _, item := range changeConf { - if strings.HasPrefix(line, item.key) || strings.HasPrefix(line, "# "+item.key) { - if item.value == "0" || len(item.value) == 0 { - newFiles = append(newFiles, fmt.Sprintf("# %s %s", item.key, item.value)) - } else { - newFiles = append(newFiles, fmt.Sprintf("%s %s", item.key, item.value)) - } - isExist = true - break - } - } - if isExist { - continue - } - newFiles = append(newFiles, line) - if line == "# End Redis configuration rewrite by 1Panel" { - isEndRange = true - } - continue + if files[i] == "# End Redis configuration rewrite by 1Panel" { + endIndex = i + break } - newFiles = append(newFiles, line) + newFiles = append(newFiles, files[i]) } + for _, item := range changeConf { + isExist := false + for i := startIndex; i < endIndex; i++ { + if strings.HasPrefix(newFiles[i], item.key) || strings.HasPrefix(newFiles[i], "# "+item.key) { + if item.value == "0" || len(item.value) == 0 { + newFiles[i] = fmt.Sprintf("# %s %s", item.key, item.value) + } else { + newFiles[i] = fmt.Sprintf("%s %s", item.key, item.value) + } + isExist = true + break + } + } + if !isExist { + if item.value == "0" || len(item.value) == 0 { + newFiles = append(newFiles, fmt.Sprintf("# %s %s", item.key, item.value)) + } else { + newFiles = append(newFiles, fmt.Sprintf("%s %s", item.key, item.value)) + } + } + } + newFiles = append(newFiles, "# End Redis configuration rewrite by 1Panel") + file, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0666) if err != nil { return err