-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
70 lines (58 loc) · 1.21 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"github.com/cavaliergopher/grab/v3"
"github.com/gofiber/fiber/v3/log"
"github.com/spf13/viper"
"os"
"sync"
"time"
)
// 单例结构体
type DataManager struct {
data EPG
mutex sync.Mutex
}
// 全局单例变量
var instance *DataManager
var once sync.Once
// 获取单例实例
func GetDataManagerInstance() *DataManager {
once.Do(func() {
instance = &DataManager{
data: makeData(),
}
// 启动一个协程定时更新数据
go instance.updateDataPeriodically()
})
return instance
}
// 定时更新数据的函数
func (dm *DataManager) updateDataPeriodically() {
ticker := time.NewTicker(2 * time.Hour) // 每5秒更新一次数据
for {
select {
case <-ticker.C:
dm.mutex.Lock()
dm.data = makeData()
dm.mutex.Unlock()
}
}
}
// 获取数据
func (dm *DataManager) GetData() EPG {
dm.mutex.Lock()
defer dm.mutex.Unlock()
return dm.data
}
func makeData() EPG {
log.Debugf("开始更新XML文件,时间 - %s", time.Now().String())
var file = "epg.xml"
//删除老的文件
_ = os.Remove(file)
resp, err := grab.Get(file, viper.GetString("epg_url"))
if err != nil {
log.Error("下载XML文件失败", err)
return EPG{}
}
return parseXML(resp.Filename)
}