From 1cd83f4e879db5f016a51ee5d4300976aef46c00 Mon Sep 17 00:00:00 2001 From: LinkinStar Date: Fri, 28 Oct 2022 20:24:04 +0800 Subject: [PATCH] feat: change default cache dir to data, create dir if not exist. --- configs/config.yaml | 2 +- internal/base/data/data.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/configs/config.yaml b/configs/config.yaml index 3e7cfd84..0ec1a601 100644 --- a/configs/config.yaml +++ b/configs/config.yaml @@ -6,7 +6,7 @@ data: driver: "mysql" connection: root:root@tcp(db:3306)/answer cache: - file_path: "/tmp/cache/cache.db" + file_path: "/data/cache/cache.db" i18n: bundle_dir: "/data/i18n" swaggerui: diff --git a/internal/base/data/data.go b/internal/base/data/data.go index cb50f7e0..b6878181 100644 --- a/internal/base/data/data.go +++ b/internal/base/data/data.go @@ -1,8 +1,10 @@ package data import ( + "path/filepath" "time" + "github.com/answerdev/answer/pkg/dir" _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" _ "github.com/mattn/go-sqlite3" @@ -69,6 +71,12 @@ func NewCache(c *CacheConf) (cache.Cache, func(), error) { memCache := memory.NewCache() if len(c.FilePath) > 0 { + cacheFileDir := filepath.Dir(c.FilePath) + log.Debugf("try to create cache directory %s", cacheFileDir) + err := dir.CreateDirIfNotExist(cacheFileDir) + if err != nil { + log.Errorf("create cache dir failed: %s", err) + } log.Infof("try to load cache file from %s", c.FilePath) if err := memory.Load(memCache, c.FilePath); err != nil { log.Warn(err)