diff --git a/backend/app/service/database_redis.go b/backend/app/service/database_redis.go index 91ca335fc..6937cd8a0 100644 --- a/backend/app/service/database_redis.go +++ b/backend/app/service/database_redis.go @@ -44,7 +44,7 @@ func (u *RedisService) UpdateConf(req dto.RedisConfUpdate) error { confs = append(confs, redisConfig{key: "timeout", value: req.Timeout}) confs = append(confs, redisConfig{key: "maxclients", value: req.Maxclients}) confs = append(confs, redisConfig{key: "maxmemory", value: req.Maxmemory}) - if err := confSet(redisInfo.Name, confs); err != nil { + if err := confSet(redisInfo.Name, "", confs); err != nil { return err } if _, err := compose.Restart(fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)); err != nil { @@ -78,7 +78,7 @@ func (u *RedisService) UpdatePersistenceConf(req dto.RedisConfPersistenceUpdate) confs = append(confs, redisConfig{key: "appendonly", value: req.Appendonly}) confs = append(confs, redisConfig{key: "appendfsync", value: req.Appendfsync}) } - if err := confSet(redisInfo.Name, confs); err != nil { + if err := confSet(redisInfo.Name, req.Type, confs); err != nil { return err } if _, err := compose.Restart(fmt.Sprintf("%s/redis/%s/docker-compose.yml", constant.AppInstallDir, redisInfo.Name)); err != nil { @@ -213,7 +213,7 @@ type redisConfig struct { value string } -func confSet(redisName string, changeConf []redisConfig) error { +func confSet(redisName string, updateType string, changeConf []redisConfig) error { path := fmt.Sprintf("%s/redis/%s/conf/redis.conf", constant.AppInstallDir, redisName) lineBytes, err := os.ReadFile(path) if err != nil { @@ -221,19 +221,38 @@ func confSet(redisName string, changeConf []redisConfig) error { } files := strings.Split(string(lineBytes), "\n") - startIndex, endIndex := 0, 0 + startIndex, endIndex, emptyLine := 0, 0, 0 var newFiles []string for i := 0; i < len(files); i++ { if files[i] == "# Redis configuration rewrite by 1Panel" { startIndex = i + newFiles = append(newFiles, files[i]) + continue } if files[i] == "# End Redis configuration rewrite by 1Panel" { endIndex = i break } + if startIndex == 0 && strings.HasPrefix(files[i], "save ") { + newFiles = append(newFiles, "# "+files[i]) + continue + } + if startIndex != 0 && endIndex == 0 && (len(files[i]) == 0 || (updateType == "rbd" && strings.HasPrefix(files[i], "save "))) { + emptyLine++ + continue + } newFiles = append(newFiles, files[i]) } + endIndex = endIndex - emptyLine for _, item := range changeConf { + if item.key == "save" { + saveVal := strings.Split(item.value, ",") + for i := 0; i < len(saveVal); i++ { + newFiles = append(newFiles, "save "+saveVal[i]) + } + continue + } + isExist := false for i := startIndex; i < endIndex; i++ { if strings.HasPrefix(newFiles[i], item.key) || strings.HasPrefix(newFiles[i], "# "+item.key) { diff --git a/frontend/src/views/database/redis/setting/persistence/index.vue b/frontend/src/views/database/redis/setting/persistence/index.vue index 972b2cba9..16d6c363b 100644 --- a/frontend/src/views/database/redis/setting/persistence/index.vue +++ b/frontend/src/views/database/redis/setting/persistence/index.vue @@ -292,10 +292,10 @@ const onSave = async (formEl: FormInstance | undefined, type: string) => { MsgInfo(i18n.global.t('database.rdbInfo')); return; } - itemSaves.push(item.second + '', item.count + ''); + itemSaves.push(item.second + ' ' + item.count); } param.type = type; - param.save = itemSaves.join(' '); + param.save = itemSaves.join(','); emit('loading', true); await updateRedisPersistenceConf(param) .then(() => {