move refetch code to function, call on stale or no data
parent
cd2da0aa5c
commit
7aa82bead7
29
main.go
29
main.go
|
@ -43,19 +43,9 @@ func main() {
|
|||
|
||||
var id uint32 = 29932586
|
||||
data, ok := cache[id]
|
||||
if !ok {
|
||||
log.Printf("Fetching fresh data for %d\n", id)
|
||||
s := godestone.NewScraper(bingode.New(), godestone.EN)
|
||||
|
||||
c, err := s.FetchCharacter(id)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
raw_data, _ := json.Marshal(c.ClassJobs)
|
||||
data = CacheRecord{Time: time.Now(), Data: string(raw_data)}
|
||||
|
||||
cache[id] = data
|
||||
if !ok || time.Now().After(data.Time.Add(time.Minute*10)) {
|
||||
cache[id] = refetch_data(id)
|
||||
data = cache[id]
|
||||
}
|
||||
|
||||
cache_as_stored, err := json.Marshal(cache)
|
||||
|
@ -69,3 +59,16 @@ func main() {
|
|||
}
|
||||
fmt.Printf("%s\n", data.Data)
|
||||
}
|
||||
|
||||
func refetch_data(id uint32) CacheRecord {
|
||||
log.Printf("Fetching fresh data for %d\n", id)
|
||||
s := godestone.NewScraper(bingode.New(), godestone.EN)
|
||||
|
||||
c, err := s.FetchCharacter(id)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
raw_data, _ := json.Marshal(c.ClassJobs)
|
||||
return CacheRecord{Time: time.Now(), Data: string(raw_data)}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue