fix:修复旧版本没有标识文档读写缓存失败的BUG

pull/358/head
lifei6671 2018-08-13 15:04:52 +08:00
parent b50d39f690
commit c7251697b3
7 changed files with 60 additions and 44 deletions

View File

@ -84,6 +84,9 @@ WORKDIR /mindoc
COPY --from=0 /go/src/github.com/lifei6671/mindoc .
# 时区设置
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ENV ZONEINFO=/mindoc/lib/time/zoneinfo.zip
RUN chmod +x start.sh

23
cache/cache.go vendored
View File

@ -9,16 +9,17 @@ import (
"github.com/astaxie/beego"
)
var bm cache.Cache
func Get(key string,e interface{}) error {
func Get(key string, e interface{}) error {
val := bm.Get(key)
if val == nil {
return errors.New("cache does not exist")
}
if b,ok := val.([]byte); ok {
if b, ok := val.([]byte); ok {
buf := bytes.NewBuffer(b)
decoder := gob.NewDecoder(buf)
@ -29,7 +30,7 @@ func Get(key string,e interface{}) error {
beego.Error("反序列化对象失败 ->", err)
}
return err
}else if s,ok := val.(string); ok && s != "" {
} else if s, ok := val.(string); ok && s != "" {
buf := bytes.NewBufferString(s)
@ -38,16 +39,13 @@ func Get(key string,e interface{}) error {
err := decoder.Decode(e)
if err != nil {
beego.Error("反序列化对象失败 ->", err)
}
beego.Error("反序列化对象失败 ->", err)
}
return err
}
return errors.New("value is not []byte or string")
}
func GetMulti(keys []string) []interface{} {
return bm.GetMulti(keys)
}
func Put(key string, val interface{}, timeout time.Duration) error {
@ -57,7 +55,7 @@ func Put(key string, val interface{}, timeout time.Duration) error {
err := encoder.Encode(val)
if err != nil {
beego.Error("序列化对象失败 ->",err)
beego.Error("序列化对象失败 ->", err)
return err
}
@ -76,14 +74,15 @@ func Decr(key string) error {
func IsExist(key string) bool {
return bm.IsExist(key)
}
func ClearAll() error{
func ClearAll() error {
return bm.ClearAll()
}
func StartAndGC(config string) error {
return bm.StartAndGC(config)
}
//初始化缓存
func Init(c cache.Cache) {
func Init(c cache.Cache) {
bm = c
}
}

2
cache/cache_null.go vendored
View File

@ -7,7 +7,7 @@ type NullCache struct {
}
func (bm *NullCache)Get(key string) interface{} {
func (bm *NullCache) Get(key string) interface{} {
return nil
}

View File

@ -43,7 +43,7 @@ func RegisterDataBase() {
if err == nil {
orm.DefaultTimeLoc = location
} else {
beego.Error("加载时区配置信息失败,请检查是否存在ZONEINFO环境变量->", err)
beego.Error("加载时区配置信息失败,请检查是否存在 ZONEINFO 环境变量->", err)
}
port := beego.AppConfig.String("db_port")
@ -108,7 +108,12 @@ func RegisterLogger(log string) {
logs.Async(1e3)
}
if log == "" {
log = conf.WorkingDir("runtime","logs")
logPath,err := filepath.Abs(beego.AppConfig.DefaultString("log_path",conf.WorkingDir("runtime","logs")))
if err == nil {
log = logPath
}else{
log = conf.WorkingDir("runtime","logs")
}
}
logPath := filepath.Join(log, "log.log")
@ -235,9 +240,7 @@ func ResolveCommand(args []string) {
conf.WorkingDirectory = filepath.Dir(p)
}
}
if conf.LogFile == "" {
conf.LogFile = conf.WorkingDir("runtime","logs")
}
if conf.ConfigurationFile == "" {
conf.ConfigurationFile = conf.WorkingDir( "conf", "app.conf")
config := conf.WorkingDir("conf", "app.conf.example")
@ -252,6 +255,15 @@ func ResolveCommand(args []string) {
if err := beego.LoadAppConfig("ini", conf.ConfigurationFile);err != nil {
log.Fatal("An error occurred:", err)
}
if conf.LogFile == "" {
logPath,err := filepath.Abs(beego.AppConfig.DefaultString("log_path",conf.WorkingDir("runtime","logs")))
if err == nil {
conf.LogFile = logPath
}else{
conf.LogFile = conf.WorkingDir("runtime","logs")
}
}
conf.AutoLoadDelay = beego.AppConfig.DefaultInt("config_auto_delay",0)
uploads := conf.WorkingDir("uploads")
@ -282,6 +294,7 @@ func RegisterCache() {
isOpenCache := beego.AppConfig.DefaultBool("cache", false)
if !isOpenCache {
cache.Init(&cache.NullCache{})
return
}
beego.Info("正常初始化缓存配置.")
cacheProvider := beego.AppConfig.String("cache_provider")
@ -355,13 +368,13 @@ func RegisterCache() {
bc, err := json.Marshal(&memcacheConfig)
if err != nil {
beego.Error("初始化Redis缓存失败:", err)
beego.Error("初始化 Redis 缓存失败 ->", err)
os.Exit(1)
}
memcache, err := beegoCache.NewCache("memcache", string(bc))
if err != nil {
beego.Error("初始化Memcache缓存失败:", err)
beego.Error("初始化 Memcache 缓存失败 ->", err)
os.Exit(1)
}
@ -397,10 +410,13 @@ func RegisterAutoLoadConfig() {
}
if modTime != f.ModTime() {
if err := beego.LoadAppConfig("ini", conf.ConfigurationFile); err != nil {
beego.Error("An error occurred:", err)
beego.Error("An error occurred ->", err)
break
}
modTime = f.ModTime()
RegisterCache()
RegisterLogger("")
beego.Info("配置文件已加载")
}
}

View File

@ -89,15 +89,15 @@ func (m *Document) InsertOrUpdate(cols ...string) error {
if m.Identify == "" {
book := NewBook()
identify := "docs"
if err := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id",m.BookId).One(book,"identify");err == nil {
if err := o.QueryTable(book.TableNameWithPrefix()).Filter("book_id", m.BookId).One(book, "identify"); err == nil {
identify = book.Identify
}
m.Identify = fmt.Sprintf("%s-%s",identify,strconv.FormatInt(time.Now().UnixNano(), 32))
m.Identify = fmt.Sprintf("%s-%s", identify, strconv.FormatInt(time.Now().UnixNano(), 32))
}
if m.OrderSort == 0{
sort,_ := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id",m.BookId).Filter("parent_id",m.ParentId).Count()
if m.OrderSort == 0 {
sort, _ := o.QueryTable(m.TableNameWithPrefix()).Filter("book_id", m.BookId).Filter("parent_id", m.ParentId).Count()
m.OrderSort = int(sort) + 1
}
_, err = o.Insert(m)
@ -151,16 +151,16 @@ func (m *Document) RecursiveDocument(docId int) error {
func (m *Document) PutToCache() {
go func(m Document) {
if m.Identify == "" {
if m.Identify == "" {
if err := cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), m, time.Second*3600); err != nil {
beego.Info("文档缓存失败:", m.DocumentId)
}
} else {
if err := cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), m, time.Second*3600); err != nil {
beego.Info("文档缓存失败:", m.DocumentId)
}
if err := cache.Put("Document.Id."+strconv.Itoa(m.DocumentId), m, time.Second*3600); err != nil {
beego.Info("文档缓存失败:", m.DocumentId)
}
} else {
if err := cache.Put(fmt.Sprintf("Document.BookId.%d.Identify.%s", m.BookId, m.Identify), m, time.Second*3600); err != nil {
beego.Info("文档缓存失败:", m.DocumentId)
}
}
}(*m)
}
@ -179,9 +179,7 @@ func (m *Document) RemoveCache() {
//从缓存获取
func (m *Document) FromCacheById(id int) (*Document, error) {
var doc Document
if err := cache.Get("Document.Id."+strconv.Itoa(id), &m); err == nil && m.DocumentId > 0 {
m = &doc
beego.Info("从缓存中获取文档信息成功 ->", m.DocumentId)
return m, nil
}
@ -189,12 +187,12 @@ func (m *Document) FromCacheById(id int) (*Document, error) {
if m.DocumentId > 0 {
m.PutToCache()
}
m,err := m.Find(id)
m, err := m.Find(id)
if err == nil {
m.PutToCache()
}
return m,err
return m, err
}
//根据文档标识从缓存中查询文档
@ -202,7 +200,7 @@ func (m *Document) FromCacheByIdentify(identify string, bookId int) (*Document,
key := fmt.Sprintf("Document.BookId.%d.Identify.%s", bookId, identify)
if err := cache.Get(key,m); err == nil && m.DocumentId > 0 {
if err := cache.Get(key, m); err == nil && m.DocumentId > 0 {
beego.Info("从缓存中获取文档信息成功 ->", key)
return m, nil
}
@ -228,5 +226,5 @@ func (m *Document) FindListByBookId(bookId int) (docs []*Document, err error) {
func (m *Document) IsExist(documentId int) bool {
o := orm.NewOrm()
return o.QueryTable(m.TableNameWithPrefix()).Filter("document_id",documentId).Exist()
}
return o.QueryTable(m.TableNameWithPrefix()).Filter("document_id", documentId).Exist()
}

View File

@ -5,8 +5,6 @@ cd /mindoc/
if [ ! -f "/mindoc/conf/app.conf" ] ; then
cp /mindoc/conf/app.conf.example /mindoc/conf/app.conf
sed -i "s#^db_adapter=.*#db_adapter=sqlite3#g" conf/app.conf
sed -i "s#^db_database.*#db_database=./database/mindoc.db#g" conf/app.conf
fi

View File

@ -1,11 +1,13 @@
<div class="footer">
<div class="container">
<div class="row text-center border-top">
<span><a href="https://www.iminho.me" target="_blank">MinDoc</a></span>
<span><a href="https://www.iminho.me" target="_blank">官方网站</a></span>
<span>&nbsp;·&nbsp;</span>
<span><a href="https://github.com/lifei6671/mindoc/issues" target="_blank">意见反馈</a></span>
<span>&nbsp;·&nbsp;</span>
<span><a href="https://github.com/lifei6671/mindoc" target="_blank">Github</a></span>
<span><a href="https://github.com/lifei6671/mindoc" target="_blank">项目源码</a></span>
<span>&nbsp;·&nbsp;</span>
<span><a href="https://www.iminho.me/wiki/docs/mindoc/" target="_blank">使用手册</a></span>
</div>
{{if ne .site_beian ""}}
<div class="row text-center">