-
Notifications
You must be signed in to change notification settings - Fork 787
/
cache.go
72 lines (63 loc) · 1.77 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
package util
import (
"fmt"
"io/ioutil"
"time"
"github.com/jenkins-x/jx/pkg/log"
)
const (
timeLayout = time.RFC1123
defaultFileWritePermisons = 0644
// TODO make this configurable?
defaultCacheTimeoutHours = 24
)
type CacheLoader func() ([]byte, error)
// LoadCacheData loads cached data from the given cache file name and loader
func LoadCacheData(fileName string, loader CacheLoader) ([]byte, error) {
if fileName == "" {
return loader()
}
timecheckFileName := fileName + "_last_time_check"
exists, _ := FileExists(fileName)
if exists {
// lets check if we should use cache
if shouldUseCache(timecheckFileName) {
return ioutil.ReadFile(fileName)
}
}
data, err := loader()
if err == nil {
err2 := ioutil.WriteFile(fileName, data, defaultFileWritePermisons)
if err2 != nil {
log.Warnf("Failed to update cache file %s due to %s", fileName, err2)
}
writeTimeToFile(timecheckFileName, time.Now())
}
return data, err
}
// shouldUseCache returns true if we should use the cached data to serve up the content
func shouldUseCache(filePath string) bool {
lastUpdateTime := getTimeFromFileIfExists(filePath)
if time.Since(lastUpdateTime).Hours() < defaultCacheTimeoutHours {
return true
}
return false
}
func writeTimeToFile(path string, inputTime time.Time) error {
err := ioutil.WriteFile(path, []byte(inputTime.Format(timeLayout)), defaultFileWritePermisons)
if err != nil {
return fmt.Errorf("Error writing current update time to file: %s", err)
}
return nil
}
func getTimeFromFileIfExists(path string) time.Time {
lastUpdateCheckTime, err := ioutil.ReadFile(path)
if err != nil {
return time.Time{}
}
timeInFile, err := time.Parse(timeLayout, string(lastUpdateCheckTime))
if err != nil {
return time.Time{}
}
return timeInFile
}