diff --git a/internal/api/v1/favicon/cache.go b/internal/api/v1/favicon/cache.go index d138854..b6764a8 100644 --- a/internal/api/v1/favicon/cache.go +++ b/internal/api/v1/favicon/cache.go @@ -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++ } } - logging.Info().Int("pruned", nPruned).Msg("pruned expired icon cache") + if nPruned > 0 { + logging.Info().Int("pruned", nPruned).Msg("pruned expired icon cache") + } } func routeKey(r route.HTTPRoute) string { diff --git a/internal/homepage/override_config.go b/internal/homepage/override_config.go index c8ac779..d11d7e5 100644 --- a/internal/homepage/override_config.go +++ b/internal/homepage/override_config.go @@ -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 { diff --git a/internal/list-icons.go b/internal/list-icons.go index 18f341c..1f3d2c6 100644 --- a/internal/list-icons.go +++ b/internal/list-icons.go @@ -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() diff --git a/internal/utils/serialization.go b/internal/utils/serialization.go index 0cc4f98..7f9251e 100644 --- a/internal/utils/serialization.go +++ b/internal/utils/serialization.go @@ -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 - } - return err +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 loadSerialized(path, dst, json.Unmarshal) + return true, err }