-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
50 lines (43 loc) · 795 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
43
44
45
46
47
48
49
50
package main
import (
"bufio"
"encoding/json"
"errors"
"os"
"time"
)
const (
cacheFileName = ".ghls_cache"
)
var (
errCacheIsExpired = errors.New("cache is expired")
)
func readCache(p string) ([]repository, error) {
fi, err := os.Stat(p)
if err != nil {
return nil, err
}
// check cache file is flesh.
if time.Since(fi.ModTime()) >= 24*time.Hour {
return nil, errCacheIsExpired
}
f, err := os.Open(p)
if err != nil {
return nil, err
}
defer f.Close()
bufs := make([]repository, 0)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
var r repository
b := scanner.Text()
if err := json.Unmarshal([]byte(b), &r); err != nil {
return nil, err
}
bufs = append(bufs, r)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return bufs, nil
}