-
Notifications
You must be signed in to change notification settings - Fork 17
/
cache.go
42 lines (33 loc) · 772 Bytes
/
cache.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
package main
import (
"math/rand"
"time"
)
//A structure created for caching pages for a given amount of time. This avoids heavy traffic to the HN servers.
type PageCache struct {
Created time.Time `json:"created"`
Pages map[string]*Page `json:"pages"`
Articles []*Article `json:"articles"`
next string
Next string `json:"next"`
}
func RandomString() string {
rand.Seed(time.Now().Unix())
b := make([]byte, 80)
return string(b)
}
func NewPageCache() *PageCache {
pc := PageCache{
Next: "news",
Pages: make(map[string]*Page),
}
pc.GetNext()
return &pc
}
func (pc *PageCache) GetNext() *Page {
p := NewPage(pc.Next)
pc.Pages[p.Url] = p
pc.Next = p.NextUrl
pc.Articles = append(pc.Articles, p.Articles...)
return p
}