1 Star 1 Fork 0

fast_api/api

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cache.go 1.84 KB
一键复制 编辑 原始数据 按行查看 历史
t.io 提交于 2023-10-31 21:53 +08:00 . Fix test case
package cache
import (
"bytes"
"io"
"reflect"
"time"
"gitee.com/fast_api/api/call"
"gitee.com/fast_api/api/def"
"gitee.com/fast_api/api/http"
)
var (
cacheType = reflect.TypeOf((*Cache)(nil)).Elem()
persistenceCache PersistenceCache = &defaultPersistenceCache{}
processCache ProcessCache = &defaultProcessCacheImpl{}
)
func init() {
http.RegisterReturnHandler(&Bytes{})
call.SetMethodProxy(func(fn call.MethodCaller, m *def.MethodInfo, args []reflect.Value) []reflect.Value {
key := []byte(m.MethodName + "@")
encodeKey := processCache.EncodeKey(m, args)
if len(encodeKey) > 0 {
key = append(key, encodeKey...)
}
if cv := persistenceCache.Get(key); cv != nil {
return []reflect.Value{
reflect.ValueOf(Bytes(cv)),
}
}
invokeReturn := fn.Invoke(m, args)
if len(invokeReturn) > 1 {
cache := validCache(invokeReturn)
if cache != nil {
persistenceCache.Set(key, processCache.EncodeValue(def.DefaultContext.Serialize, invokeReturn[0]), cache.ExpireTime_())
}
}
return invokeReturn
})
}
func validArgs(m *def.MethodInfo, args []reflect.Value) {
}
func validCache(vs []reflect.Value) Cache {
for _, v := range vs {
if !v.IsValid() || v.IsNil() {
continue
}
if v.Type().Implements(cacheType) {
return v.Interface().(Cache)
}
}
return nil
}
type Bytes []byte
func (b Bytes) Return() io.Reader {
return bytes.NewReader(b)
}
func (b Bytes) Register() []reflect.Type {
return []reflect.Type{
reflect.TypeOf((*Bytes)(nil)).Elem(),
}
}
func (b Bytes) ContentType() string {
return def.Content_JSON
}
type Cache interface {
ExpireTime_() time.Duration
}
type Key interface {
}
type ImplCache struct {
time time.Duration
}
func NewCacheImpl(time time.Duration) *ImplCache {
return &ImplCache{time: time}
}
func (c *ImplCache) ExpireTime_() time.Duration {
return c.time
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/fast_api/api.git
git@gitee.com:fast_api/api.git
fast_api
api
api
v0.1.0

搜索帮助