-
Notifications
You must be signed in to change notification settings - Fork 0
/
static.go
94 lines (83 loc) · 1.85 KB
/
static.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package static
import (
"context"
"encoding/json"
"strconv"
"time"
"go-common/app/interface/main/app-resource/conf"
eggdao "go-common/app/interface/main/app-resource/dao/egg"
"go-common/app/interface/main/app-resource/model"
"go-common/app/interface/main/app-resource/model/static"
"go-common/library/ecode"
"go-common/library/log"
farm "github.com/dgryski/go-farm"
)
const (
_initVersion = "static_version"
)
var (
_emptyStatics = []*static.Static{}
)
// Service static service.
type Service struct {
dao *eggdao.Dao
tick time.Duration
cache map[int8][]*static.Static
staticPath string
}
// New new a static service.
func New(c *conf.Config) (s *Service) {
s = &Service{
dao: eggdao.New(c),
tick: time.Duration(c.Tick),
cache: map[int8][]*static.Static{},
staticPath: c.StaticJSONFile,
}
now := time.Now()
s.loadCache(now)
go s.loadCachepro()
return
}
// Static return statics
func (s *Service) Static(plat int8, build int, ver string, now time.Time) (res []*static.Static, version string, err error) {
var (
tmps = s.cache[plat]
)
for _, tmp := range tmps {
if model.InvalidBuild(build, tmp.Build, tmp.Condition) {
continue
}
res = append(res, tmp)
}
if len(res) == 0 {
res = _emptyStatics
}
if version = s.hash(res); version == ver {
err = ecode.NotModified
res = nil
}
return
}
func (s *Service) hash(v []*static.Static) string {
bs, err := json.Marshal(v)
if err != nil {
log.Error("json.Marshal error(%v)", err)
return _initVersion
}
return strconv.FormatUint(farm.Hash64(bs), 10)
}
// loadCache update egg
func (s *Service) loadCache(now time.Time) {
tmp, err := s.dao.Egg(context.TODO(), now)
if err != nil {
log.Error("s.dao.Egg error(%v)", err)
return
}
s.cache = tmp
}
func (s *Service) loadCachepro() {
for {
time.Sleep(s.tick)
s.loadCache(time.Now())
}
}