mirror of https://github.com/mindoc-org/mindoc.git
优化文件缓存
parent
8d71f2fa7d
commit
e9a78f0aa7
|
@ -258,18 +258,30 @@ func RegisterCache() {
|
||||||
beego.Info("正常初始化缓存配置.")
|
beego.Info("正常初始化缓存配置.")
|
||||||
cacheProvider := beego.AppConfig.String("cache_provider")
|
cacheProvider := beego.AppConfig.String("cache_provider")
|
||||||
if cacheProvider == "file" {
|
if cacheProvider == "file" {
|
||||||
cacheFilePath := beego.AppConfig.DefaultString("cache_file_path","./runtime/")
|
cacheFilePath := beego.AppConfig.DefaultString("cache_file_path","./runtime/cache/")
|
||||||
if strings.HasPrefix(cacheFilePath, "./") {
|
if strings.HasPrefix(cacheFilePath, "./") {
|
||||||
cacheFilePath = filepath.Join(conf.WorkingDirectory, string(cacheFilePath[1:]))
|
cacheFilePath = filepath.Join(conf.WorkingDirectory, string(cacheFilePath[1:]))
|
||||||
}
|
}
|
||||||
fileCache := beegoCache.NewFileCache()
|
fileCache := beegoCache.NewFileCache()
|
||||||
beegoCache.FileCachePath = cacheFilePath
|
|
||||||
beegoCache.FileCacheDirectoryLevel = beego.AppConfig.DefaultInt("cache_file_dir_level",2)
|
|
||||||
beegoCache.FileCacheEmbedExpiry = time.Duration(beego.AppConfig.DefaultInt64("cache_file_expiry",120))
|
fileConfig := make(map[string]string,0)
|
||||||
beegoCache.FileCacheFileSuffix = beego.AppConfig.DefaultString("cache_file_suffix",".bin")
|
|
||||||
fileCache.StartAndGC("")
|
fileConfig["CachePath"] = cacheFilePath
|
||||||
|
fileConfig["DirectoryLevel"] = beego.AppConfig.DefaultString("cache_file_dir_level","2")
|
||||||
|
fileConfig["EmbedExpiry"] = beego.AppConfig.DefaultString("cache_file_expiry","120")
|
||||||
|
fileConfig["FileSuffix"] = beego.AppConfig.DefaultString("cache_file_suffix",".bin")
|
||||||
|
|
||||||
|
bc,err := json.Marshal(&fileConfig)
|
||||||
|
if err != nil {
|
||||||
|
beego.Error("初始化Redis缓存失败:",err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fileCache.StartAndGC(string(bc))
|
||||||
|
|
||||||
cache.Init(fileCache)
|
cache.Init(fileCache)
|
||||||
|
|
||||||
}else if cacheProvider == "memory" {
|
}else if cacheProvider == "memory" {
|
||||||
cacheInterval := beego.AppConfig.DefaultInt("cache_memory_interval",60)
|
cacheInterval := beego.AppConfig.DefaultInt("cache_memory_interval",60)
|
||||||
memory := beegoCache.NewMemoryCache()
|
memory := beegoCache.NewMemoryCache()
|
||||||
|
|
|
@ -113,7 +113,7 @@ cache_provider=memory
|
||||||
#当配置缓存方式为memory时,内存回收时间,单位是秒
|
#当配置缓存方式为memory时,内存回收时间,单位是秒
|
||||||
cache_memory_interval=120
|
cache_memory_interval=120
|
||||||
#当缓存方式配置为file时,缓存的储存目录
|
#当缓存方式配置为file时,缓存的储存目录
|
||||||
cache_file_path=./runtime/
|
cache_file_path=./runtime/cache/
|
||||||
#缓存文件后缀
|
#缓存文件后缀
|
||||||
cache_file_suffix=.bin
|
cache_file_suffix=.bin
|
||||||
#文件缓存目录层级
|
#文件缓存目录层级
|
||||||
|
|
|
@ -240,6 +240,11 @@ func (m *Document) FromCacheById(id int) (*Document,error) {
|
||||||
return m,nil
|
return m,nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
|
if m.DocumentId > 0 {
|
||||||
|
m.PutToCache()
|
||||||
|
}
|
||||||
|
}()
|
||||||
return m.Find(id)
|
return m.Find(id)
|
||||||
}
|
}
|
||||||
//根据文档标识从缓存中查询文档
|
//根据文档标识从缓存中查询文档
|
||||||
|
@ -247,10 +252,15 @@ func (m *Document) FromCacheByIdentify(identify string) (*Document,error) {
|
||||||
b := cache.Get("Document.Identify." + identify)
|
b := cache.Get("Document.Identify." + identify)
|
||||||
if v,ok := b.([]byte); ok {
|
if v,ok := b.([]byte); ok {
|
||||||
if err := json.Unmarshal(v,m);err == nil{
|
if err := json.Unmarshal(v,m);err == nil{
|
||||||
beego.Info("从缓存中获取文档信息成功",m.DocumentId)
|
beego.Info("从缓存中获取文档信息成功",m.DocumentId,identify)
|
||||||
return m,nil
|
return m,nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
|
if m.DocumentId > 0 {
|
||||||
|
m.PutToCache()
|
||||||
|
}
|
||||||
|
}()
|
||||||
return m.FindByFieldFirst("identify",identify)
|
return m.FindByFieldFirst("identify",identify)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue