From ef8affe24e1d1dccd4c066c8e51488199e9105a9 Mon Sep 17 00:00:00 2001 From: u Date: Fri, 21 Apr 2023 11:42:45 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=EF=BC=9A=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=86=97=E4=BD=99=E4=BB=A3=E7=A0=81=E5=B9=B6=E6=94=B9=E8=BF=9B?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - - 在GetHolidaysDataWithCache函数中删除多余的锁定操作 - 在GetHolidaysDataWithCache函数中删除不必要的panic操作 - 在cache_test文件中添加缓存过期的测试。 Signed-off-by: u --- cache_test.go | 5 +++++ core.go | 5 ----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cache_test.go b/cache_test.go index 1dd2252..a3e762e 100644 --- a/cache_test.go +++ b/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) } diff --git a/core.go b/core.go index 22687aa..110af5a 100644 --- a/core.go +++ b/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 }