-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
115 lines (93 loc) · 2.9 KB
/
file.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
package timeline
import (
"encoding/json"
"fmt"
"os"
"path"
"time"
"github.com/pkg/errors"
)
type FileCacheOptions[Data any, Key any] struct {
CacheDir string
KeyToStr func(Key) string
GetTimestamp func(d *Data) time.Time
GetFromSource CacheSource[Data, Key]
SkipDataVerification bool
}
func NewFileCache[Data any, Key any](opts FileCacheOptions[Data, Key]) (*FileCache[Data, Key], error) {
if opts.CacheDir == "" {
return nil, errors.New("cache directory is not set")
}
if err := os.MkdirAll(opts.CacheDir, 0755); err != nil && !os.IsExist(err) {
return nil, errors.Wrapf(err, "failed to create cache directory %v", opts.CacheDir)
}
keyToStr := opts.KeyToStr
if keyToStr == nil {
keyToStr = CreateConvertorToString[Key]()
}
c := &FileCache[Data, Key]{
cacheDir: opts.CacheDir,
keyToStr: keyToStr,
}
c.MemoryCache = *NewMemoryCache[Data, Key](MemoryCacheOptions[Data, Key]{
KeyToStr: opts.KeyToStr,
GetTimestamp: opts.GetTimestamp,
GetFromSource: opts.GetFromSource,
Load: c.loadCacheFromFile,
Save: c.saveCacheToFile,
SkipDataVerification: opts.SkipDataVerification,
})
return c, nil
}
type FileCache[Data any, Key any] struct {
cacheDir string
keyToStr func(Key) string
MemoryCache[Data, Key]
}
var _ Cache[struct{}, int64] = &FileCache[struct{}, int64]{}
func (c *FileCache[Data, Key]) cacheFilePath(key Key) string {
return path.Join(c.cacheDir, fmt.Sprintf("cache_%v.json", c.keyToStr(key)))
}
func (c *FileCache[Data, Key]) loadCacheFromFile(key Key) (*MemoryCacheState[Data], error) {
cacheFilePath := c.cacheFilePath(key)
cacheBytes, err := os.ReadFile(cacheFilePath)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
if len(cacheBytes) == 0 {
return nil, nil
}
var dump []*CacheStateSegment[Data]
if err := json.Unmarshal(cacheBytes, &dump); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal cache from file %v: %v", cacheFilePath, string(cacheBytes))
}
return &MemoryCacheState[Data]{
Segments: dump,
}, nil
}
func (c *FileCache[Data, Key]) saveCacheToFile(key Key, state *CacheState[Data], _ []*CacheStateSegment[Data]) error {
var dump []*CacheStateSegment[Data]
for _, segment := range state.Segments {
data, err := segment.Data.Get(segment.PeriodStart, segment.PeriodEnd)
if err != nil {
return err
}
dump = append(dump, &CacheStateSegment[Data]{
PeriodStart: segment.PeriodStart,
PeriodEnd: segment.PeriodEnd,
Data: data,
})
}
cacheFilePath := c.cacheFilePath(key)
cacheBytes, err := json.MarshalIndent(dump, "", " ")
if err != nil {
return errors.Wrapf(err, "failed to marshal cache to file %v", cacheFilePath)
}
if err := os.WriteFile(cacheFilePath, cacheBytes, 0644); err != nil {
return errors.Wrapf(err, "failed to write cache to file %v", cacheFilePath)
}
return nil
}