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:
parent
567e8cfc89
commit
f9dce3d10f
1 changed files with 11 additions and 0 deletions
|
@ -4,9 +4,14 @@
|
|||
package timezone // import "miniflux.app/v2/internal/timezone"
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
tzCache = sync.Map{} // Cache for time locations to avoid loading them multiple times.
|
||||
)
|
||||
|
||||
// Convert converts provided date time to actual timezone.
|
||||
func Convert(tz string, t time.Time) time.Time {
|
||||
userTimezone := getLocation(tz)
|
||||
|
@ -42,9 +47,15 @@ func Now(tz string) time.Time {
|
|||
}
|
||||
|
||||
func getLocation(tz string) *time.Location {
|
||||
if loc, ok := tzCache.Load(tz); ok {
|
||||
return loc.(*time.Location)
|
||||
}
|
||||
|
||||
loc, err := time.LoadLocation(tz)
|
||||
if err != nil {
|
||||
loc = time.Local
|
||||
}
|
||||
|
||||
tzCache.Store(tz, loc)
|
||||
return loc
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue