feat: change default cache dir to data, create dir if not exist.

This commit is contained in:
LinkinStar 2022-10-28 20:24:04 +08:00
parent 10a06573d5
commit 1cd83f4e87
2 changed files with 9 additions and 1 deletions

View File

@ -6,7 +6,7 @@ data:
driver: "mysql" driver: "mysql"
connection: root:root@tcp(db:3306)/answer connection: root:root@tcp(db:3306)/answer
cache: cache:
file_path: "/tmp/cache/cache.db" file_path: "/data/cache/cache.db"
i18n: i18n:
bundle_dir: "/data/i18n" bundle_dir: "/data/i18n"
swaggerui: swaggerui:

View File

@ -1,8 +1,10 @@
package data package data
import ( import (
"path/filepath"
"time" "time"
"github.com/answerdev/answer/pkg/dir"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq" _ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
@ -69,6 +71,12 @@ func NewCache(c *CacheConf) (cache.Cache, func(), error) {
memCache := memory.NewCache() memCache := memory.NewCache()
if len(c.FilePath) > 0 { 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) log.Infof("try to load cache file from %s", c.FilePath)
if err := memory.Load(memCache, c.FilePath); err != nil { if err := memory.Load(memCache, c.FilePath); err != nil {
log.Warn(err) log.Warn(err)