refactor: improved json loading flow and log messages

This commit is contained in:
yusing 2025-03-28 07:00:21 +08:00
parent 8c03c5e82e
commit c0c6e21a16
4 changed files with 25 additions and 24 deletions

View file

@ -24,7 +24,7 @@ var (
)
const (
iconCacheTTL = 24 * time.Hour
iconCacheTTL = 3 * 24 * time.Hour
cleanUpInterval = time.Hour
)
@ -35,8 +35,8 @@ func InitIconCache() {
err := utils.LoadJSONIfExist(common.IconCachePath, &iconCache)
if err != nil {
logging.Error().Err(err).Msg("failed to load icon cache")
} else {
logging.Info().Msgf("icon cache loaded (%d icons)", len(iconCache))
} else if len(iconCache) > 0 {
logging.Info().Int("count", len(iconCache)).Msg("icon cache loaded")
}
go func() {
@ -77,7 +77,9 @@ func pruneExpiredIconCache() {
nPruned++
}
}
if nPruned > 0 {
logging.Info().Int("pruned", nPruned).Msg("pruned expired icon cache")
}
}
func routeKey(r route.HTTPRoute) string {

View file

@ -31,8 +31,10 @@ func InitOverridesConfig() {
err := utils.LoadJSONIfExist(common.HomepageJSONConfigPath, overrideConfigInstance)
if err != nil {
logging.Error().Err(err).Msg("failed to load homepage overrides config")
} else {
logging.Info().Msgf("homepage overrides config loaded, %d items", len(overrideConfigInstance.ItemOverrides))
} else if len(overrideConfigInstance.ItemOverrides) > 0 {
logging.Info().
Int("count", len(overrideConfigInstance.ItemOverrides)).
Msg("homepage overrides config loaded")
}
task.OnProgramExit("save_homepage_json_config", func() {
if len(overrideConfigInstance.ItemOverrides) == 0 {

View file

@ -4,7 +4,6 @@ import (
"encoding/json"
"io"
"net/http"
"os"
"sync"
"time"
@ -12,7 +11,6 @@ import (
"github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/logging"
"github.com/yusing/go-proxy/internal/utils"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
type GitHubContents struct { //! keep this, may reuse in future
@ -64,12 +62,11 @@ func InitIconListCache() {
err := utils.LoadJSONIfExist(common.IconListCachePath, iconsCache)
if err != nil {
logging.Error().Err(err).Msg("failed to load icon list cache config")
} else if stats, err := os.Stat(common.IconListCachePath); err == nil {
lastUpdate = stats.ModTime()
logging.Info().Msgf("icon list cache loaded (%d icons, %d display names), last updated at %s",
len(iconsCache.IconList),
len(iconsCache.DisplayNames),
strutils.FormatTime(lastUpdate))
} else if len(iconsCache.IconList) > 0 {
logging.Info().
Int("icons", len(iconsCache.IconList)).
Int("display_names", len(iconsCache.DisplayNames)).
Msg("icon list cache loaded")
}
}
@ -86,12 +83,15 @@ func ListAvailableIcons() (*Cache, error) {
iconsCahceMu.Lock()
defer iconsCahceMu.Unlock()
logging.Info().Msg("updating icon data")
icons, err := fetchIconData()
if err != nil {
return nil, err
}
logging.Info().Msg("icons list updated")
logging.Info().
Int("icons", len(icons.IconList)).
Int("display_names", len(icons.DisplayNames)).
Msg("icons list updated")
iconsCache = icons
lastUpdate = time.Now()

View file

@ -529,13 +529,10 @@ func SaveJSON[T any](path string, src *T, perm os.FileMode) error {
return os.WriteFile(path, data, perm)
}
func LoadJSONIfExist[T any](path string, dst *T) error {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return nil
func LoadJSONIfExist[T any](path string, dst *T) (exists bool, err error) {
err = loadSerialized(path, dst, json.Unmarshal)
if err != nil && os.IsNotExist(err) {
return false, nil
}
return err
}
return loadSerialized(path, dst, json.Unmarshal)
return true, err
}