-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.go
157 lines (140 loc) · 2.9 KB
/
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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"encoding/json"
"fmt"
"os"
"sync"
"time"
"github.com/zippoxer/george/forge"
)
type cache struct {
client *forge.Client
servers []forge.Server
serversMu sync.Mutex
sites map[int][]forge.Site // Map of server id to it's sites.
sitesMu sync.Mutex
}
func newCache(client *forge.Client) *cache {
return &cache{
client: client,
sites: make(map[int][]forge.Site),
}
}
func (c *cache) Servers() ([]forge.Server, error) {
c.serversMu.Lock()
ok := c.servers != nil
c.serversMu.Unlock()
if ok {
return c.servers, nil
}
servers, err := c.client.Servers().List()
if err != nil {
return nil, err
}
c.serversMu.Lock()
c.servers = servers
c.serversMu.Unlock()
return servers, nil
}
func (c *cache) Server(id int) (*forge.Server, error) {
servers, err := c.Servers()
if err != nil {
return nil, err
}
c.serversMu.Lock()
defer c.serversMu.Unlock()
for _, server := range servers {
if server.Id == id {
return &server, nil
}
}
return nil, fmt.Errorf("Server not found.")
}
func (c *cache) Sites(serverId int) ([]forge.Site, error) {
c.sitesMu.Lock()
sites, ok := c.sites[serverId]
c.sitesMu.Unlock()
if ok {
return sites, nil
}
sites, err := c.client.Sites(serverId).List()
if err != nil {
return nil, err
}
c.sitesMu.Lock()
c.sites[serverId] = sites
c.sitesMu.Unlock()
return sites, nil
}
type ServerSites struct {
forge.Server
Sites []forge.Site
}
func (c *cache) ServerSites(servers []forge.Server) ([]ServerSites, error) {
if servers == nil {
var err error
servers, err = c.Servers()
if err != nil {
return nil, err
}
}
dataChan := make(chan ServerSites)
errChan := make(chan error, len(servers))
for _, server := range servers {
go func(server forge.Server) {
sites, err := c.Sites(server.Id)
if err != nil {
errChan <- err
}
dataChan <- ServerSites{Server: server, Sites: sites}
}(server)
}
results := make([]ServerSites, 0, len(servers))
for i := 0; i < len(servers); i++ {
select {
case data := <-dataChan:
results = append(results, data)
case err := <-errChan:
return nil, err
}
}
return results, nil
}
type cacheDump struct {
Updated time.Time
Servers []forge.Server
Sites map[int][]forge.Site
}
var cacheForever = time.Duration(-1)
func (c *cache) Load(fileName string, maxAge time.Duration) error {
f, err := os.Open(fileName)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return err
}
var dump cacheDump
if err := json.NewDecoder(f).Decode(&dump); err != nil {
return err
}
if maxAge != cacheForever && time.Since(dump.Updated) > maxAge {
return nil
}
c.servers = dump.Servers
c.sites = dump.Sites
return nil
}
func (c *cache) Dump(fileName string) error {
f, err := os.Create(fileName)
if err != nil {
return err
}
defer f.Close()
dump := cacheDump{
Updated: time.Now(),
Servers: c.servers,
Sites: c.sites,
}
return json.NewEncoder(f).Encode(dump)
}