实现文档缓存

This commit is contained in:
Minho
2018-02-27 17:20:42 +08:00
parent 405a9c309f
commit 849058ff8a
6 changed files with 323 additions and 38 deletions

View File

@@ -20,10 +20,15 @@ import (
"github.com/lifei6671/mindoc/conf"
"github.com/lifei6671/mindoc/models"
"github.com/lifei6671/mindoc/utils"
"github.com/lifei6671/mindoc/cache"
beegoCache "github.com/astaxie/beego/cache"
_ "github.com/astaxie/beego/cache/memcache"
_ "github.com/astaxie/beego/cache/redis"
)
// RegisterDataBase 注册数据库
func RegisterDataBase() {
beego.Info("正在初始化数据库配置.")
adapter := beego.AppConfig.String("db_adapter")
if adapter == "mysql" {
@@ -37,13 +42,18 @@ func RegisterDataBase() {
dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=true&loc=%s", username, password, host, port, database, url.QueryEscape(timezone))
orm.RegisterDataBase("default", "mysql", dataSource)
err := orm.RegisterDataBase("default", "mysql", dataSource)
if err != nil {
beego.Error("注册默认数据库失败:",err)
os.Exit(1)
}
location, err := time.LoadLocation(timezone)
if err == nil {
orm.DefaultTimeLoc = location
} else {
log.Fatalln(err)
beego.Error("加载时区配置信息失败,请检查是否存在ZONEINFO环境变量:",err)
os.Exit(1)
}
} else if adapter == "sqlite3" {
database := beego.AppConfig.String("db_database")
@@ -54,8 +64,17 @@ func RegisterDataBase() {
dbPath := filepath.Dir(database)
os.MkdirAll(dbPath, 0777)
orm.RegisterDataBase("default", "sqlite3", database)
err := orm.RegisterDataBase("default", "sqlite3", database)
if err != nil {
beego.Error("注册默认数据库失败:",err)
os.Exit(1)
}
}else{
beego.Error("不支持的数据库类型.")
os.Exit(1)
}
beego.Info("数据库初始化完成.")
}
// RegisterModel 注册Model
@@ -120,7 +139,7 @@ func RegisterCommand() {
migrate.RunMigration()
}
}
//注册模板函数
func RegisterFunction() {
beego.AddFuncMap("config", models.GetOptionValue)
@@ -179,6 +198,7 @@ func RegisterFunction() {
})
}
//解析命令
func ResolveCommand(args []string) {
flagSet := flag.NewFlagSet("MinDoc command: ", flag.ExitOnError)
flagSet.StringVar(&conf.ConfigurationFile, "config", "", "MinDoc configuration file.")
@@ -226,10 +246,95 @@ func ResolveCommand(args []string) {
gocaptcha.ReadFonts(filepath.Join(conf.WorkingDirectory, "static", "fonts"), ".ttf")
RegisterDataBase()
RegisterCache()
RegisterModel()
RegisterLogger(conf.LogFile)
}
//注册缓存管道
func RegisterCache() {
isOpenCache := beego.AppConfig.DefaultBool("cache",false)
if !isOpenCache {
cache.Init(&cache.NullCache{})
}
beego.Info("正常初始化缓存配置.")
cacheProvider := beego.AppConfig.String("cache_provider")
if cacheProvider == "file" {
cacheFilePath := beego.AppConfig.DefaultString("cache_file_path","./runtime/")
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("")
cache.Init(fileCache)
}else if cacheProvider == "memory" {
cacheInterval := beego.AppConfig.DefaultInt("cache_memory_interval",60)
memory := beegoCache.NewMemoryCache()
beegoCache.DefaultEvery = cacheInterval
cache.Init(memory)
}else if cacheProvider == "redis"{
var redisConfig struct{
Conn string `json:"conn"`
Password string `json:"password"`
DbNum int `json:"dbNum"`
}
redisConfig.DbNum = 0
redisConfig.Conn = beego.AppConfig.DefaultString("cache_redis_host","")
if pwd := beego.AppConfig.DefaultString("cache_redis_password","");pwd != "" {
redisConfig.Password = pwd
}
if dbNum := beego.AppConfig.DefaultInt("cache_redis_db",0); dbNum > 0 {
redisConfig.DbNum = dbNum
}
bc,err := json.Marshal(&redisConfig)
if err != nil {
beego.Error("初始化Redis缓存失败:",err)
os.Exit(1)
}
beego.Info(string(bc))
redisCache,err := beegoCache.NewCache("redis",string(bc))
if err != nil {
beego.Error("初始化Redis缓存失败:",err)
os.Exit(1)
}
cache.Init(redisCache)
}else if cacheProvider == "memcache" {
var memcacheConfig struct{
Conn string `json:"conn"`
}
memcacheConfig.Conn = beego.AppConfig.DefaultString("cache_memcache_host","")
bc,err := json.Marshal(&memcacheConfig)
if err != nil {
beego.Error("初始化Redis缓存失败:",err)
os.Exit(1)
}
memcache,err := beegoCache.NewCache("memcache",string(bc))
if err != nil {
beego.Error("初始化Memcache缓存失败:",err)
os.Exit(1)
}
cache.Init(memcache)
}else {
cache.Init(&cache.NullCache{})
beego.Warn("不支持的缓存管道,缓存将禁用.")
return
}
beego.Info("缓存初始化完成.")
}
func init() {
if configPath ,err := filepath.Abs(conf.ConfigurationFile); err == nil {