Files
KOMATA 5209275f28 feat: Implement caching for settings retrieval and update logic (#11886)
* feat: Implement caching for settings retrieval and update logic

- Introduced a caching mechanism for settings using go-cache to improve performance.
- Updated the Create, Update, and UpdateOrCreate methods to cache values after database operations.
- Modified the Get and GetValueByKey methods to utilize the cache for faster access.
- Adjusted middleware to use the new GetValueByKey method for retrieving settings, enhancing code consistency.

* feat: Enhance setting cache management with dynamic TTL

- Introduced a function to determine cache TTL based on setting keys, allowing critical settings to have shorter cache durations.
- Updated cache logic in Create, Update, Get, and GetValueByKey methods to utilize the new TTL management, improving performance and consistency in settings retrieval.

* refactor: Simplify setting cache TTL management

- Removed the dynamic TTL function and standardized the cache TTL to a fixed duration for all settings.
- Updated cache logic in Create, Update, Get, and GetValueByKey methods to use the new fixed TTL, enhancing code clarity and maintainability.
- Added a new function to restart the core service after resetting settings, improving the reset command's functionality.

* refactor: Remove core restart functionality from reset commands

- Eliminated the restartCoreAfterReset function to simplify the reset command logic.
- Updated reset commands to return nil after setting changes, enhancing clarity and reducing unnecessary complexity.
2026-02-25 16:40:20 +08:00

49 lines
1.3 KiB
Go

package middleware
import (
"strconv"
"strings"
"github.com/1Panel-dev/1Panel/core/app/api/v2/helper"
"github.com/1Panel-dev/1Panel/core/app/repo"
"github.com/1Panel-dev/1Panel/core/buserr"
"github.com/1Panel-dev/1Panel/core/constant"
"github.com/1Panel-dev/1Panel/core/global"
"github.com/gin-gonic/gin"
)
func SessionAuth() gin.HandlerFunc {
return func(c *gin.Context) {
apiReq := c.GetBool("API_AUTH")
if strings.HasPrefix(c.Request.URL.Path, "/api/v2/core/auth") || apiReq {
c.Next()
return
}
psession, err := global.SESSION.Get(c)
if err != nil {
errItem := err.Error()
if errItem == "ErrSessionDataFormat" || errItem == "ErrSessionDataNotFound" {
helper.BadAuth(c, "ErrNotLogin", buserr.New(errItem))
return
}
helper.BadAuth(c, "ErrNotLogin", err)
return
}
settingRepo := repo.NewISettingRepo()
sessionTimeout, err := settingRepo.GetValueByKey("SessionTimeout")
if err != nil {
global.LOG.Errorf("create operation record failed, err: %v", err)
return
}
lifeTime, _ := strconv.Atoi(sessionTimeout)
ssl, err := settingRepo.GetValueByKey("SSL")
if err != nil {
global.LOG.Errorf("create operation record failed, err: %v", err)
return
}
_ = global.SESSION.Set(c, psession, ssl == constant.StatusEnable, lifeTime)
c.Next()
}
}