summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiguel Mota <hello@miguelmota.com>2021-04-25 12:18:53 -0700
committerMiguel Mota <hello@miguelmota.com>2021-04-25 12:18:53 -0700
commit9fab6bd5b9d381047f4349c5bf4bc60800c7da47 (patch)
tree02c0ef3a6ee52657a11a3fc5244f7d359bb88818
parent7b73406c9fe43e4a13588573cfb0b5bf129a3468 (diff)
File cache fixv1.6.4
-rw-r--r--pkg/filecache/filecache.go15
1 files changed, 8 insertions, 7 deletions
diff --git a/pkg/filecache/filecache.go b/pkg/filecache/filecache.go
index 78519a3..e446ce8 100644
--- a/pkg/filecache/filecache.go
+++ b/pkg/filecache/filecache.go
@@ -64,11 +64,11 @@ func (f *FileCache) Set(key string, data interface{}, expire time.Duration) erro
defer f.muts[key].Unlock()
key = regexp.MustCompile("[^a-zA-Z0-9_-]").ReplaceAllLiteralString(key, "")
- var prefix string
if f.prefix != "" {
- prefix = fmt.Sprintf("%s.", f.prefix)
+ key = fmt.Sprintf("%s.%s", f.prefix, key)
}
- file := fmt.Sprintf("fcache.%s%s.%v", prefix, key, strconv.FormatInt(time.Now().Add(expire).Unix(), 10))
+ ts := strconv.FormatInt(time.Now().Add(expire).Unix(), 10)
+ file := fmt.Sprintf("fcache.%s.%v", key, ts)
fpath := filepath.Join(f.cacheDir, file)
f.clean(key)
@@ -97,11 +97,10 @@ func (f *FileCache) Set(key string, data interface{}, expire time.Duration) erro
// Get reads item from cache
func (f *FileCache) Get(key string, dst interface{}) error {
key = regexp.MustCompile("[^a-zA-Z0-9_-]").ReplaceAllLiteralString(key, "")
- var prefix string
if f.prefix != "" {
- prefix = fmt.Sprintf("%s.", f.prefix)
+ key = fmt.Sprintf("%s.%s", f.prefix, key)
}
- pattern := filepath.Join(f.cacheDir, fmt.Sprintf("fcache.%s%s.*", prefix, key))
+ pattern := filepath.Join(f.cacheDir, fmt.Sprintf("fcache.%s.*", key))
files, err := filepath.Glob(pattern)
if len(files) < 1 || err != nil {
return errors.New("fcache: no cache file found")
@@ -133,7 +132,9 @@ func (f *FileCache) Get(key string, dst interface{}) error {
}
for _, file := range files {
- exptime, err := strconv.ParseInt(strings.Split(file, ".")[2], 10, 64)
+ parts := strings.Split(file, ".")
+ ts := parts[len(parts)-1]
+ exptime, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
return err
}