-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcache.go
More file actions
56 lines (47 loc) · 1.24 KB
/
Copy pathcache.go
File metadata and controls
56 lines (47 loc) · 1.24 KB
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
package main
import (
"context"
"time"
"github.com/allegro/bigcache/v3"
)
var (
AUTHCACHE *bigcache.BigCache
FILECACHE *bigcache.BigCache
)
func initCache() {
var err error
AUTHCACHE, err = bigcache.New(context.Background(), bigcache.DefaultConfig(10*time.Minute))
if err != nil {
panic(err)
}
FILECACHE, err = bigcache.New(context.Background(), bigcache.DefaultConfig(1*time.Hour))
if err != nil {
panic(err)
}
}
// caches a file, return error if saving to cache fails and forwards bigcache error
func cacheFile(filename string, file []byte) error {
err := FILECACHE.Set(filename, file)
if err != nil {
return err
}
return nil
}
// Return cached file if it exists in cache, otherwise returns ErrFileNotFound. If an error happens in retrieval it forwards bigcache error
func getCachedFileIfExists(filename string) ([]byte, error) {
entry, err := FILECACHE.Get(filename)
if err == bigcache.ErrEntryNotFound {
return nil, ErrFileNotFound
} else if err != nil {
return nil, err
}
return entry, nil
}
// removes a cached file, returns error if file is not found in cache or if an error happens in removal
func removeCachedFile(filename string) error {
err := FILECACHE.Delete(filename)
if err != nil {
return err
}
return nil
}