add CacheDiscovery

This commit is contained in:
smallnest
2023-10-31 22:59:46 +08:00
parent 39c51c0855
commit b73eba8fea

View File

@@ -3,6 +3,7 @@ package client
import ( import (
"encoding/json" "encoding/json"
"os" "os"
"path/filepath"
"sync" "sync"
) )
@@ -39,6 +40,15 @@ func CacheDiscovery(threshold int, cachedFile string, discovery ServiceDiscovery
cachedFile = ".cache/discovery.json" cachedFile = ".cache/discovery.json"
} }
cachedFileDir := filepath.Dir(cachedFile)
if _, err := os.Stat(cachedFileDir); os.IsNotExist(err) {
// 目录不存在,创建目录
if err := os.MkdirAll(cachedFileDir, os.ModePerm); err != nil {
panic(err)
}
}
return &cachedServiceDiscovery{ return &cachedServiceDiscovery{
threshold: threshold, threshold: threshold,
cachedFile: cachedFile, cachedFile: cachedFile,