-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
49 lines (39 loc) · 1.03 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
package main
import (
"fmt"
"hash/fnv"
"io/ioutil"
"path"
"time"
"github.com/spf13/viper"
)
type cacheItem struct {
location string
filename string
}
func newCacheItem(location string) *cacheItem {
// create a hash for the specified location to be used as the file name
hash := fnv.New64a()
hash.Write([]byte(location))
return &cacheItem{
location: location,
filename: fmt.Sprintf("%x", hash.Sum64()),
}
}
func (cache *cacheItem) exists() bool {
return fileExists(cache.path())
}
func (cache *cacheItem) outOfDate() bool {
if lastModified, err := fileModifiedTime(cache.path()); err == nil {
// determine whether the last time the file was modified was longer than
// the configured cache length
return time.Now().Sub(lastModified).Seconds() > float64(viper.GetInt(cacheLengthKey))
}
return true
}
func (cache *cacheItem) path() string {
return path.Join(viper.GetString(cacheDirectoryKey), cache.filename)
}
func (cache *cacheItem) save(data []byte) error {
return ioutil.WriteFile(cache.path(), data, 0644)
}