Skip to content

Commit

Permalink
File cache fix
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Apr 25, 2021
1 parent 7b73406 commit 9fab6bd
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pkg/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 9fab6bd

Please sign in to comment.