Add filecache prefix based on config file path

pull/112/head^2
Miguel Mota 3 years ago
parent e0acaf9554
commit 6a4aac1277
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9

@ -1,6 +1,7 @@
package cointop
import (
"crypto/sha256"
"fmt"
"io/ioutil"
"os"
@ -307,8 +308,11 @@ func NewCointop(config *Config) (*Cointop, error) {
}
if !config.NoCache {
// each custom config file has it's own file cache
hash := sha256.Sum256([]byte(ct.ConfigFilePath()))
fcache, err := filecache.NewFileCache(&filecache.Config{
CacheDir: ct.State.cacheDir,
Prefix: fmt.Sprintf("%x", hash[0:4]),
})
if err != nil {
fmt.Printf("error: %s\nyou may change the cache directory with --cache-dir flag.\nproceeding without filecache.\n", err)

@ -21,12 +21,14 @@ var DefaultCacheDir = "/tmp"
// FileCache ...
type FileCache struct {
muts map[string]*sync.Mutex
prefix string
cacheDir string
}
// Config ...
type Config struct {
CacheDir string
Prefix string
}
// NewFileCache ...
@ -39,7 +41,6 @@ func NewFileCache(config *Config) (*FileCache, error) {
if config.CacheDir != "" {
cacheDir = config.CacheDir
}
if _, err := os.Stat(cacheDir); os.IsNotExist(err) {
if err := os.MkdirAll(cacheDir, 0700); err != nil {
return nil, err
@ -49,6 +50,7 @@ func NewFileCache(config *Config) (*FileCache, error) {
return &FileCache{
muts: make(map[string]*sync.Mutex),
cacheDir: cacheDir,
prefix: config.Prefix,
}, nil
}
@ -62,7 +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, "")
file := fmt.Sprintf("fcache.%s.%v", key, strconv.FormatInt(time.Now().Add(expire).Unix(), 10))
var prefix string
if f.prefix != "" {
prefix = fmt.Sprintf("%s.", f.prefix)
}
file := fmt.Sprintf("fcache.%s%s.%v", prefix, key, strconv.FormatInt(time.Now().Add(expire).Unix(), 10))
fpath := filepath.Join(f.cacheDir, file)
f.clean(key)
@ -91,7 +97,11 @@ 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, "")
pattern := filepath.Join(f.cacheDir, fmt.Sprintf("fcache.%s.*", key))
var prefix string
if f.prefix != "" {
prefix = fmt.Sprintf("%s.", f.prefix)
}
pattern := filepath.Join(f.cacheDir, fmt.Sprintf("fcache.%s%s.*", prefix, key))
files, err := filepath.Glob(pattern)
if len(files) < 1 || err != nil {
return errors.New("fcache: no cache file found")

Loading…
Cancel
Save