Просмотр исходного кода

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
jvoisin 10 месяцев назад
Родитель
Сommit
f9dce3d10f
1 измененных файлов с 11 добавлено и 0 удалено
  1. 11 0
      internal/timezone/timezone.go

+ 11 - 0
internal/timezone/timezone.go

@@ -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
 }