修复使用文件做缓存时无法反序列化的BUG

pull/358/head
lifei6671 2018-07-25 14:56:46 +08:00
parent 1cbdd4baca
commit b732cbbdc8
2 changed files with 19 additions and 5 deletions

17
cache/cache.go vendored
View File

@ -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 errors.New("value is not []byte")
return err
}
return errors.New("value is not []byte or string")
}
func GetMulti(keys []string) []interface{} {

View File

@ -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)