-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
57 lines (46 loc) · 1.07 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
package main
import (
"bufio"
"fmt"
"log"
"os"
)
type cache struct {
fileStore *os.File
}
func NewCache(cachePath string) *cache {
// load cache file on each run
f, err := os.OpenFile(cachePath, os.O_RDWR|os.O_CREATE, 0775)
if err != nil {
log.Fatalf("could not open cache file: %v\n", err)
}
return &cache{f}
}
func (c *cache) loadCache() map[string]struct{} {
result := make(map[string]struct{})
scanner := bufio.NewScanner(c.fileStore)
for scanner.Scan() {
result[scanner.Text()] = struct{}{}
}
fmt.Printf("loading cache: %d lines\n", len(result))
return result
}
func (c *cache) writeCache(cacheBuildIDs map[string]struct{}) error {
err := c.fileStore.Truncate(0)
if err != nil {
return fmt.Errorf("error truncating cache: %v", err)
}
_, err = c.fileStore.Seek(0, 0)
if err != nil {
return fmt.Errorf("error seeking cache: %v", err)
}
w := bufio.NewWriter(c.fileStore)
defer w.Flush()
for k := range cacheBuildIDs {
_, err := w.WriteString(k + "\n")
if err != nil {
return fmt.Errorf("error writing cache: %v", err)
}
}
return nil
}