From e9a78f0aa7ab0e0039552f51fe0dcf37598fd3a1 Mon Sep 17 00:00:00 2001 From: Minho Date: Thu, 1 Mar 2018 10:12:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E7=BC=93?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/command.go | 24 ++++++++++++++++++------ conf/app.conf.example | 2 +- models/document.go | 12 +++++++++++- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/commands/command.go b/commands/command.go index dc5e273b..02a00cdf 100644 --- a/commands/command.go +++ b/commands/command.go @@ -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() diff --git a/conf/app.conf.example b/conf/app.conf.example index 78529ec0..72acf61e 100644 --- a/conf/app.conf.example +++ b/conf/app.conf.example @@ -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 #文件缓存目录层级 diff --git a/models/document.go b/models/document.go index 621f1533..420eac58 100644 --- a/models/document.go +++ b/models/document.go @@ -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) }