Browse Source

重构:删除冗余代码并改进缓存测试

-
- 在GetHolidaysDataWithCache函数中删除多余的锁定操作
- 在GetHolidaysDataWithCache函数中删除不必要的panic操作
- 在cache_test文件中添加缓存过期的测试。

Signed-off-by: u <u@a.com>
master
u 1 year ago
parent
commit
ef8affe24e
  1. 5
      cache_test.go
  2. 5
      core.go

5
cache_test.go

@ -7,6 +7,11 @@ import (
func Test_cache_IsExpired(t *testing.T) {
cachet := cache{}
if !cachet.IsExpired() {
t.Error("cache should be expired")
}
if cachet.IsExpired() {
cachet.SetData("hello", time.Second*5)
}

5
core.go

@ -380,8 +380,6 @@ 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 {
@ -392,9 +390,6 @@ func GetHolidaysDataWithCache() ([]HolidaysItem, error) {
data := holidayCache.GetData()
var result []HolidaysItem
err := json.Unmarshal([]byte(data), &result)
if len(data) == 0 {
panic("GetHolidaysDataWithCache json unmarshal error")
}
return result, err
}

Loading…
Cancel
Save