From 7885302987e691be10f809b095432446cd2ea0f1 Mon Sep 17 00:00:00 2001 From: u Date: Fri, 21 Apr 2023 11:17:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=9A=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E5=8A=9F=E8=83=BD=E4=BB=A5=E6=94=B9=E5=96=84?= =?UTF-8?q?=E6=A0=B8=E5=BF=83=E9=83=A8=E5=88=86=E4=B8=AD=E7=9A=84=E5=81=87?= =?UTF-8?q?=E6=97=A5=E6=95=B0=E6=8D=AE=E6=A3=80=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加一个新文件'cache.go' - 在'core.go'中将函数'GetHolidaysData'更改为'GetHolidaysDataWithCache' - 添加一个缓存结构和相关函数到'core.go',用于缓存假日数据 - 在'core.go'中添加一个函数'jsonEncode',用于编码JSON数据。 Signed-off-by: u --- cache.go | 24 ++++++++++++++++++++++++ core.go | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 cache.go 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) }