1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-06-27 16:36:00 +00:00

perf(timzone): cache getLocation's results

Every time getLocation is called, it's opening and parsing a file on disc,
sometimes a zip file depending on the system. We can cache the results instead
of doing this.

See https://github.com/golang/go/issues/24844 and https://github.com/golang/go/issues/26106
This commit is contained in:
jvoisin 2025-06-08 16:12:02 +02:00 committed by Frédéric Guillot
parent 567e8cfc89
commit f9dce3d10f

View file

@ -4,9 +4,14 @@
package timezone // import "miniflux.app/v2/internal/timezone" package timezone // import "miniflux.app/v2/internal/timezone"
import ( import (
"sync"
"time" "time"
) )
var (
tzCache = sync.Map{} // Cache for time locations to avoid loading them multiple times.
)
// Convert converts provided date time to actual timezone. // Convert converts provided date time to actual timezone.
func Convert(tz string, t time.Time) time.Time { func Convert(tz string, t time.Time) time.Time {
userTimezone := getLocation(tz) userTimezone := getLocation(tz)
@ -42,9 +47,15 @@ func Now(tz string) time.Time {
} }
func getLocation(tz string) *time.Location { func getLocation(tz string) *time.Location {
if loc, ok := tzCache.Load(tz); ok {
return loc.(*time.Location)
}
loc, err := time.LoadLocation(tz) loc, err := time.LoadLocation(tz)
if err != nil { if err != nil {
loc = time.Local loc = time.Local
} }
tzCache.Store(tz, loc)
return loc return loc
} }