Browse Source
功能:添加缓存功能以改善核心部分中的假日数据检索
功能:添加缓存功能以改善核心部分中的假日数据检索
- 添加一个新文件'cache.go' - 在'core.go'中将函数'GetHolidaysData'更改为'GetHolidaysDataWithCache' - 添加一个缓存结构和相关函数到'core.go',用于缓存假日数据 - 在'core.go'中添加一个函数'jsonEncode',用于编码JSON数据。 Signed-off-by: u <u@a.com>fast
u
2 years ago
2 changed files with 55 additions and 6 deletions
@ -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 |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue