优化文件缓存

pull/219/merge
Minho 2018-03-01 10:12:31 +08:00
parent 8d71f2fa7d
commit e9a78f0aa7
3 changed files with 30 additions and 8 deletions

View File

@ -258,18 +258,30 @@ func RegisterCache() {
beego.Info("正常初始化缓存配置.")
cacheProvider := beego.AppConfig.String("cache_provider")
if cacheProvider == "file" {
cacheFilePath := beego.AppConfig.DefaultString("cache_file_path","./runtime/")
cacheFilePath := beego.AppConfig.DefaultString("cache_file_path","./runtime/cache/")
if strings.HasPrefix(cacheFilePath, "./") {
cacheFilePath = filepath.Join(conf.WorkingDirectory, string(cacheFilePath[1:]))
}
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))
beegoCache.FileCacheFileSuffix = beego.AppConfig.DefaultString("cache_file_suffix",".bin")
fileCache.StartAndGC("")
fileConfig := make(map[string]string,0)
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)
}else if cacheProvider == "memory" {
cacheInterval := beego.AppConfig.DefaultInt("cache_memory_interval",60)
memory := beegoCache.NewMemoryCache()

View File

@ -113,7 +113,7 @@ cache_provider=memory
#当配置缓存方式为memory时,内存回收时间,单位是秒
cache_memory_interval=120
#当缓存方式配置为file时,缓存的储存目录
cache_file_path=./runtime/
cache_file_path=./runtime/cache/
#缓存文件后缀
cache_file_suffix=.bin
#文件缓存目录层级

View File

@ -240,6 +240,11 @@ func (m *Document) FromCacheById(id int) (*Document,error) {
return m,nil
}
}
defer func() {
if m.DocumentId > 0 {
m.PutToCache()
}
}()
return m.Find(id)
}
//根据文档标识从缓存中查询文档
@ -247,10 +252,15 @@ func (m *Document) FromCacheByIdentify(identify string) (*Document,error) {
b := cache.Get("Document.Identify." + identify)
if v,ok := b.([]byte); ok {
if err := json.Unmarshal(v,m);err == nil{
beego.Info("从缓存中获取文档信息成功",m.DocumentId)
beego.Info("从缓存中获取文档信息成功",m.DocumentId,identify)
return m,nil
}
}
defer func() {
if m.DocumentId > 0 {
m.PutToCache()
}
}()
return m.FindByFieldFirst("identify",identify)
}