mirror of https://github.com/mindoc-org/mindoc.git
修复使用文件做缓存时无法反序列化的BUG
parent
1cbdd4baca
commit
b732cbbdc8
|
@ -4,7 +4,6 @@ import (
|
|||
"github.com/astaxie/beego/cache"
|
||||
"time"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"bytes"
|
||||
"errors"
|
||||
"github.com/astaxie/beego"
|
||||
|
@ -27,11 +26,23 @@ func Get(key string,e interface{}) error {
|
|||
err := decoder.Decode(e)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("反序列化对象失败 ->", err)
|
||||
beego.Error("反序列化对象失败 ->", err)
|
||||
}
|
||||
return err
|
||||
}else if s,ok := val.(string); ok && s != "" {
|
||||
|
||||
buf := bytes.NewBufferString(s)
|
||||
|
||||
decoder := gob.NewDecoder(buf)
|
||||
|
||||
err := decoder.Decode(e)
|
||||
|
||||
if err != nil {
|
||||
beego.Error("反序列化对象失败 ->", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return errors.New("value is not []byte")
|
||||
return errors.New("value is not []byte or string")
|
||||
}
|
||||
|
||||
func GetMulti(keys []string) []interface{} {
|
||||
|
|
|
@ -104,11 +104,14 @@ func (b *Blog) Find(blogId int) (*Blog,error) {
|
|||
func (b *Blog) FindFromCache(blogId int) (blog *Blog,err error) {
|
||||
key := fmt.Sprintf("blog-id-%d",blogId);
|
||||
var temp Blog
|
||||
if err := cache.Get(key,&temp); err == nil {
|
||||
err = cache.Get(key,&temp);
|
||||
if err == nil {
|
||||
b = &temp
|
||||
b.Link()
|
||||
beego.Info("从缓存读取文章成功 ->", key)
|
||||
beego.Debug("从缓存读取文章成功 ->", key)
|
||||
return b,nil
|
||||
}else {
|
||||
beego.Error("读取缓存失败 ->",err)
|
||||
}
|
||||
|
||||
blog,err = b.Find(blogId)
|
||||
|
|
Loading…
Reference in New Issue