Compare commits

...

1 Commits

Author SHA1 Message Date
u 21e8334773 'fix' 2 years ago
  1. 39
      core.go

39
core.go

@ -1,6 +1,8 @@
package time_arrow package time_arrow
import ( import (
"crypto/md5"
"encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
@ -398,7 +400,8 @@ func GetHolidaysDataWithCache() ([]HolidaysItem, error) {
} }
type TimeArrowHelper struct { type TimeArrowHelper struct {
GetData func(group string) (TimeArrows, error)
GetData func(group string) (TimeArrows, error)
cacheMap map[string]*TimeArrow
} }
func (th *TimeArrowHelper) CallHitTimeArrow(t time.Time, group string, call func(*TimeArrow), expandTags ...string) error { func (th *TimeArrowHelper) CallHitTimeArrow(t time.Time, group string, call func(*TimeArrow), expandTags ...string) error {
@ -464,11 +467,36 @@ func (th *TimeArrowHelper) CallHitTimeArrow(t time.Time, group string, call func
return nil return nil
} }
func md52Str(text string) string {
h := md5.New()
h.Write([]byte(text))
return hex.EncodeToString(h.Sum(nil))
}
var hitTimeArrowCache = map[string]*TimeArrow{}
var hitTimeArrowCacheLock sync.Mutex
func (th *TimeArrowHelper) GetHitTimeArrow(t time.Time, group string, expandTags ...string) (*TimeArrow, error) { func (th *TimeArrowHelper) GetHitTimeArrow(t time.Time, group string, expandTags ...string) (*TimeArrow, error) {
ta, err := th.GetData(group) ta, err := th.GetData(group)
if err != nil { if err != nil {
return nil, err return nil, err
} }
keyA := md52Str(jsonEncode(ta))
keyB := md52Str(jsonEncode(t))
keyD := md52Str(group)
keyC := md52Str(strings.Join(expandTags, "-"))
key := fmt.Sprint(keyA, "_", keyB, "_", keyC, "_", keyD)
hitTimeArrowCacheLock.Lock()
defer hitTimeArrowCacheLock.Unlock()
if hitTimeArrowCache[key] != nil {
if hitTimeArrowCache[key].Group == "___nil" {
return nil, nil
}
return hitTimeArrowCache[key], nil
}
sort.Slice(ta, func(i, j int) bool { sort.Slice(ta, func(i, j int) bool {
return ta[i].Weights > ta[j].Weights return ta[i].Weights > ta[j].Weights
}) })
@ -487,36 +515,42 @@ func (th *TimeArrowHelper) GetHitTimeArrow(t time.Time, group string, expandTags
case TimeArrowTypeDayOfWeek: case TimeArrowTypeDayOfWeek:
//一周中某一天是否判定 //一周中某一天是否判定
if isInWeekOfDay(t, ta[e]) { if isInWeekOfDay(t, ta[e]) {
hitTimeArrowCache[key] = &ta[e]
return &ta[e], nil return &ta[e], nil
} }
break break
case TimeArrowTypeDayOfMonth: case TimeArrowTypeDayOfMonth:
//一月中某一天是否判定 //一月中某一天是否判定
if isInDayOfMonth(t, ta[e]) { if isInDayOfMonth(t, ta[e]) {
hitTimeArrowCache[key] = &ta[e]
return &ta[e], nil return &ta[e], nil
} }
break break
case TimeArrowTypeDateSlice: case TimeArrowTypeDateSlice:
//一月中某一天是否判定 //一月中某一天是否判定
if isInDateSlice(t, ta[e]) { if isInDateSlice(t, ta[e]) {
hitTimeArrowCache[key] = &ta[e]
return &ta[e], nil return &ta[e], nil
} }
break break
case TimeArrowTypeAtLastDayHolidays: case TimeArrowTypeAtLastDayHolidays:
//节假日最后一天 //节假日最后一天
if isInAtLastDayHolidays(t) { if isInAtLastDayHolidays(t) {
hitTimeArrowCache[key] = &ta[e]
return &ta[e], nil return &ta[e], nil
} }
break break
case TimeArrowTypePriorToDayHolidays: case TimeArrowTypePriorToDayHolidays:
//节假日前一天 //节假日前一天
if isInPriorToDayHolidays(t) { if isInPriorToDayHolidays(t) {
hitTimeArrowCache[key] = &ta[e]
return &ta[e], nil return &ta[e], nil
} }
break break
case TimeArrowTypeInHolidays: case TimeArrowTypeInHolidays:
//节假日 //节假日
if isInHolidays(t) { if isInHolidays(t) {
hitTimeArrowCache[key] = &ta[e]
return &ta[e], nil return &ta[e], nil
} }
break break
@ -524,5 +558,8 @@ func (th *TimeArrowHelper) GetHitTimeArrow(t time.Time, group string, expandTags
log.Println("类型错误:", ta[e].Type) log.Println("类型错误:", ta[e].Type)
} }
} }
hitTimeArrowCache[key] = &TimeArrow{
Group: "___nil",
}
return nil, nil return nil, nil
} }
Loading…
Cancel
Save