diff --git a/cache.go b/cache.go new file mode 100644 index 0000000..012871c --- /dev/null +++ b/cache.go @@ -0,0 +1,24 @@ +package time_arrow + +import "time" + +type cache struct { + Expired time.Time + Data string +} + +func (c *cache) IsExpired() bool { + return c.Expired.Before(time.Now()) +} + +func (c *cache) SetData(data string, duration time.Duration) { + c.Data = data + c.Expired = time.Now().Add(duration) +} + +func (c *cache) GetData() string { + if c.IsExpired() { + return "" + } + return c.Data +} diff --git a/core.go b/core.go index db61a79..2374be7 100644 --- a/core.go +++ b/core.go @@ -1,6 +1,7 @@ package time_arrow import ( + "encoding/json" "fmt" "log" "sort" @@ -236,7 +237,7 @@ func isInTimeOfDay2(t time.Time, ta TimeArrow) bool { } func isInHolidays(t time.Time) bool { - holidaysData, err := GetHolidaysData() + holidaysData, err := GetHolidaysDataWithCache() if err != nil { return false } @@ -253,7 +254,7 @@ func isInHolidays(t time.Time) bool { func isInAtLastDayHolidays(t time.Time) bool { t = t.Local() - holidaysData, err := GetHolidaysData() + holidaysData, err := GetHolidaysDataWithCache() if err != nil { return false } @@ -277,7 +278,7 @@ func isInAtLastDayHolidays(t time.Time) bool { } func isInPriorToDayHolidays(t time.Time) bool { - holidaysData, err := GetHolidaysData() + holidaysData, err := GetHolidaysDataWithCache() if err != nil { return false } @@ -320,7 +321,7 @@ func createUUID() string { return uid.String() } -//CreateDayOfWeekTypePlan 创建一个每周计划 +// CreateDayOfWeekTypePlan 创建一个每周计划 func CreateDayOfWeekTypePlan(group string, dayOfWeek []int, timesOnDay []string, expandValue interface{}, expandTags []string, weights float64) TimeArrow { return TimeArrow{ TimeArrowId: createUUID(), @@ -334,7 +335,7 @@ func CreateDayOfWeekTypePlan(group string, dayOfWeek []int, timesOnDay []string, } } -//CreateDayOfMonthTypePlan 创建一个每月计划 +// CreateDayOfMonthTypePlan 创建一个每月计划 func CreateDayOfMonthTypePlan(group string, dayOfMonth []int, timesOnDay []string, expandValue interface{}, expandTags []string, weights float64) TimeArrow { return TimeArrow{ TimeArrowId: createUUID(), @@ -348,7 +349,7 @@ func CreateDayOfMonthTypePlan(group string, dayOfMonth []int, timesOnDay []strin } } -//CreateDateSliceTypePlan 创建一个时间段计划 +// CreateDateSliceTypePlan 创建一个时间段计划 func CreateDateSliceTypePlan(group string, dateSlice []DateSlice, timesOnDay []string, expandValue interface{}, expandTags []string, weights float64) TimeArrow { return TimeArrow{ TimeArrowId: createUUID(), @@ -368,8 +369,32 @@ type HolidaysItem struct { End time.Time `json:"end"` } +func jsonEncode(v interface{}) string { + b, _ := json.Marshal(v) + return string(b) +} + var GetHolidaysData func() ([]HolidaysItem, error) +var holidayCache *cache +var holidayCacheLock sync.Mutex + +func GetHolidaysDataWithCache() ([]HolidaysItem, error) { + holidayCacheLock.Lock() + defer holidayCacheLock.Unlock() + if holidayCache.IsExpired() { + data, err := GetHolidaysData() + if err != nil { + return nil, err + } + holidayCache.SetData(jsonEncode(data), time.Second*60) + } + data := holidayCache.GetData() + var result []HolidaysItem + err := json.Unmarshal([]byte(data), &result) + return result, err +} + type TimeArrowHelper struct { GetData func(group string) (TimeArrows, error) }