2018-02-27 17:20:42 +08:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2018-07-25 14:46:56 +08:00
|
|
|
"bytes"
|
2021-03-23 21:55:50 +08:00
|
|
|
"context"
|
|
|
|
"encoding/gob"
|
2018-07-25 14:46:56 +08:00
|
|
|
"errors"
|
2021-03-23 21:55:50 +08:00
|
|
|
"time"
|
2018-02-27 17:20:42 +08:00
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
"github.com/beego/beego/v2/client/cache"
|
2021-04-25 16:05:05 +08:00
|
|
|
"github.com/beego/beego/v2/core/logs"
|
2021-03-23 21:55:50 +08:00
|
|
|
)
|
2018-08-13 15:04:52 +08:00
|
|
|
|
2018-02-27 17:20:42 +08:00
|
|
|
var bm cache.Cache
|
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
var nilctx = context.TODO()
|
|
|
|
|
2018-08-13 15:04:52 +08:00
|
|
|
func Get(key string, e interface{}) error {
|
2018-02-27 17:20:42 +08:00
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
val, err := bm.Get(nilctx, key)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-24 00:28:13 +08:00
|
|
|
return errors.New("get cache error:" + err.Error())
|
2021-03-23 21:55:50 +08:00
|
|
|
}
|
2018-07-25 14:46:56 +08:00
|
|
|
|
|
|
|
if val == nil {
|
|
|
|
return errors.New("cache does not exist")
|
|
|
|
}
|
2018-08-13 15:04:52 +08:00
|
|
|
if b, ok := val.([]byte); ok {
|
2018-07-25 14:46:56 +08:00
|
|
|
buf := bytes.NewBuffer(b)
|
|
|
|
|
|
|
|
decoder := gob.NewDecoder(buf)
|
|
|
|
|
|
|
|
err := decoder.Decode(e)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
logs.Error("反序列化对象失败 ->", err)
|
2018-07-25 14:46:56 +08:00
|
|
|
}
|
|
|
|
return err
|
2018-08-13 15:04:52 +08:00
|
|
|
} else if s, ok := val.(string); ok && s != "" {
|
2018-07-25 14:56:46 +08:00
|
|
|
|
|
|
|
buf := bytes.NewBufferString(s)
|
|
|
|
|
|
|
|
decoder := gob.NewDecoder(buf)
|
|
|
|
|
|
|
|
err := decoder.Decode(e)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
logs.Error("反序列化对象失败 ->", err)
|
2018-08-13 15:04:52 +08:00
|
|
|
}
|
2018-07-25 14:56:46 +08:00
|
|
|
return err
|
2018-07-25 14:46:56 +08:00
|
|
|
}
|
2018-07-25 14:56:46 +08:00
|
|
|
return errors.New("value is not []byte or string")
|
2018-02-27 17:20:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Put(key string, val interface{}, timeout time.Duration) error {
|
|
|
|
|
2018-07-25 14:46:56 +08:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
encoder := gob.NewEncoder(&buf)
|
|
|
|
|
|
|
|
err := encoder.Encode(val)
|
|
|
|
if err != nil {
|
2021-03-23 21:55:50 +08:00
|
|
|
logs.Error("序列化对象失败 ->", err)
|
2018-07-25 14:46:56 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
return bm.Put(nilctx, key, buf.String(), timeout)
|
2018-02-27 17:20:42 +08:00
|
|
|
}
|
2018-07-25 14:46:56 +08:00
|
|
|
|
2018-02-27 17:20:42 +08:00
|
|
|
func Delete(key string) error {
|
2021-03-23 21:55:50 +08:00
|
|
|
return bm.Delete(nilctx, key)
|
2018-02-27 17:20:42 +08:00
|
|
|
}
|
|
|
|
func Incr(key string) error {
|
2021-03-23 21:55:50 +08:00
|
|
|
return bm.Incr(nilctx, key)
|
2018-02-27 17:20:42 +08:00
|
|
|
}
|
|
|
|
func Decr(key string) error {
|
2021-03-23 21:55:50 +08:00
|
|
|
return bm.Decr(nilctx, key)
|
2018-02-27 17:20:42 +08:00
|
|
|
}
|
2021-03-23 21:55:50 +08:00
|
|
|
func IsExist(key string) (bool, error) {
|
|
|
|
return bm.IsExist(nilctx, key)
|
2018-02-27 17:20:42 +08:00
|
|
|
}
|
2018-08-13 15:04:52 +08:00
|
|
|
func ClearAll() error {
|
2021-03-23 21:55:50 +08:00
|
|
|
return bm.ClearAll(nilctx)
|
2018-02-27 17:20:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func StartAndGC(config string) error {
|
|
|
|
return bm.StartAndGC(config)
|
|
|
|
}
|
2018-08-13 15:04:52 +08:00
|
|
|
|
2021-03-23 21:55:50 +08:00
|
|
|
//Init will initialize cache
|
2018-08-13 15:04:52 +08:00
|
|
|
func Init(c cache.Cache) {
|
2018-02-27 17:20:42 +08:00
|
|
|
bm = c
|
2018-08-13 15:04:52 +08:00
|
|
|
}
|