/
cache.go
139 lines (102 loc) · 2.75 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
package main
import (
"crypto/sha256"
"fmt"
"io"
"log/slog"
"os"
"path"
"time"
"github.com/leighmacdonald/steamid/v4/steamid"
"github.com/pkg/errors"
)
var errCacheExpired = errors.New("cache expired")
type CacheKeyType string
const (
KeySummary CacheKeyType = "summary"
KeyBans CacheKeyType = "bans"
KeyFriends CacheKeyType = "friends"
KeyRGL = "rgl"
)
func makeKey(keyType CacheKeyType, sid64 steamid.SteamID) string {
return fmt.Sprintf("steam-%s-%d", keyType, sid64.Int64())
}
type cache interface {
get(url string) ([]byte, error)
set(key string, reader io.Reader) error
}
type nopCache struct{}
func (c *nopCache) get(_ string) ([]byte, error) {
return nil, errCacheExpired
}
func (c *nopCache) set(_ string, _ io.Reader) error {
return nil
}
func createCache(enabled bool, cacheDir string) (cache, error) {
if !enabled {
return &nopCache{}, nil
}
localCache, cacheErr := newFSCache(cacheDir)
if cacheErr != nil {
return nil, cacheErr
}
return localCache, nil
}
type fsCache struct {
cacheDir string
}
func newFSCache(cacheDir string) (*fsCache, error) {
const cachePerms = 0o755
if !exists(cacheDir) {
if errMkDir := os.MkdirAll(cacheDir, cachePerms); errMkDir != nil {
return nil, errors.Wrap(errMkDir, "Failed to create cache dir")
}
}
return &fsCache{cacheDir: cacheDir}, nil
}
func (c *fsCache) hashKey(fullURL string) (string, string) {
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(fullURL)))
dir := c.hashedPath(hash)
return dir, path.Join(dir, hash)
}
func (c *fsCache) hashedPath(hash string) string {
return path.Join(c.cacheDir, hash[0:2], hash[2:4])
}
func (c *fsCache) get(url string) ([]byte, error) {
const maxAge = time.Hour * 24 * 7
_, fullPath := c.hashKey(url)
cachedFile, errOpen := os.Open(fullPath)
if errOpen != nil {
return nil, errCacheExpired
}
stat, errStat := cachedFile.Stat()
if errStat != nil {
slog.Error("Could not stat file", ErrAttr(errStat), slog.String("file", fullPath))
return nil, errCacheExpired
}
defer logCloser(cachedFile)
if time.Since(stat.ModTime()) > maxAge {
return nil, errCacheExpired
}
body, errRead := io.ReadAll(cachedFile)
if errRead != nil {
return nil, errors.Wrap(errRead, "Failed to reach cached file")
}
return body, nil
}
func (c *fsCache) set(key string, reader io.Reader) error {
dir, fullPath := c.hashKey(key)
if errDir := os.MkdirAll(dir, os.ModePerm); errDir != nil {
return errors.Wrap(errDir, "Failed to make cache dir")
}
outFile, errOF := os.Create(fullPath)
if errOF != nil {
return errors.Wrap(errOF, "Error creating cache file")
}
defer logCloser(outFile)
_, errWrite := io.Copy(outFile, reader)
if errWrite != nil {
return errors.Wrap(errWrite, "Failed to write content to file")
}
return nil
}