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

This commit is contained in:
lifei6671
2018-08-13 15:04:52 +08:00
parent b50d39f690
commit c7251697b3
7 changed files with 60 additions and 44 deletions

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
}