-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
68 lines (56 loc) · 1.41 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
package main
import (
"encoding/json"
"fmt"
"os"
"time"
)
type cacheData struct {
DrawnNums drawnNums `json:"drawnNums"`
LastVisit time.Time `json:"lastVisit"`
NumOfDraws int `json:"numOfDraws"`
}
func read() (cacheData, error) {
data := cacheData{
DrawnNums: make(drawnNums),
}
f, err := os.OpenFile("./tmp/cache.json", os.O_RDONLY, 0644)
if err != nil {
if os.IsNotExist(err) {
return data, nil
}
return data, fmt.Errorf("failed to open cache file: %w", err)
}
defer closeFile(f)
dec := json.NewDecoder(f)
if err := dec.Decode(&data); err != nil {
return data, fmt.Errorf("failed to decode cache file: %w", err)
}
return data, nil
}
func write(numOfDraws int, drawnNums drawnNums) error {
if err := os.MkdirAll("./tmp", 0744); err != nil {
return fmt.Errorf("failed to create tmp directory: %w", err)
}
f, err := os.OpenFile("./tmp/cache.json", os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return fmt.Errorf("failed to open cache file: %w", err)
}
defer closeFile(f)
data := cacheData{
DrawnNums: drawnNums,
LastVisit: time.Now().UTC().Truncate(24 * time.Hour),
NumOfDraws: numOfDraws,
}
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
if err := enc.Encode(data); err != nil {
return fmt.Errorf("failed to write cache: %w", err)
}
return nil
}
func closeFile(f *os.File) {
if err := f.Close(); err != nil {
fmt.Println("failed to close cache file:", err)
}
}