提交 79ed5e03 authored 作者: zhanglibo's avatar zhanglibo

token redis

上级 37d326a6
......@@ -48,14 +48,19 @@ func (s *tokenDwd) Get(ctx context.Context) (res *TokenGetRes, err error) {
}
func (s *tokenDwd) Access(ctx context.Context) (res string, err error) {
cache, _ := g.Redis().DoVar("HGETALL", CacheKey)
conn := g.Redis().Ctx(ctx).Conn()
defer func() {
_ = conn.Close()
}()
_, _ = conn.DoVar("SELECT", 10)
cache, _ := conn.DoVar("HGETALL", CacheKey)
if !cache.IsEmpty() {
_ = cache.Scan(&s)
if s.ExpiresIn > gtime.Timestamp() {
res = s.AccessToken
return
}
_, err = g.Redis().DoVar("DEL", CacheKey)
_, err = conn.DoVar("DEL", CacheKey)
}
result, err := s.Get(ctx)
if err != nil {
......@@ -68,12 +73,12 @@ func (s *tokenDwd) Access(ctx context.Context) (res string, err error) {
_ = gjson.New(result.Data).Scan(&s)
s.ExpiresIn += gtime.Timestamp()
res = s.AccessToken
_, _ = g.Redis().Do("HMSET", append(g.Slice{CacheKey}, gutil.MapToSlice(gconv.Map(s))...)...)
_, _ = conn.Do("HMSET", append(g.Slice{CacheKey}, gutil.MapToSlice(gconv.Map(s))...)...)
return
}
func (s *tokenDwd) post(ctx context.Context, req interface{}) (res string, err error) {
//Start := gtime.TimestampMilli()
Start := gtime.TimestampMilli()
var request = gjson.New(req)
_ = request.Set("signature", sign(req))
......@@ -85,9 +90,9 @@ func (s *tokenDwd) post(ctx context.Context, req interface{}) (res string, err e
ctx = context.WithValue(ctx, "Method", "POST")
ctx = context.WithValue(ctx, "URI", "token")
if err != nil {
//g.Log().Ctx(ctx).Infof("参数【%v】错误【%v】响应时间【%v】", request.MustToJsonString(), err.Error(), gtime.TimestampMilli()-Start)
g.Log().Ctx(ctx).Infof("参数【%v】错误【%v】响应时间【%v】", request.MustToJsonString(), err.Error(), gtime.TimestampMilli()-Start)
} else {
//g.Log().Ctx(ctx).Cat(PkgName).Infof("参数【%v】响应【%v】响应时间【%v】", request.MustToJsonString(), res, gtime.TimestampMilli()-Start)
g.Log().Ctx(ctx).Cat(PkgName).Infof("参数【%v】响应【%v】响应时间【%v】", request.MustToJsonString(), res, gtime.TimestampMilli()-Start)
}
}()
res = resp.ReadAllString()
......
......@@ -33,7 +33,12 @@ type accessToken struct {
//getAccessToken 获取token
func (s *client) getAccessToken(ctx context.Context) (err error) {
var token *accessToken
cache, _ := g.Redis().DoVar("HGETALL", CacheKey)
conn := g.Redis().Ctx(ctx).Conn()
defer func() {
_ = conn.Close()
}()
_, _ = conn.DoVar("SELECT", 10)
cache, _ := conn.DoVar("HGETALL", CacheKey)
if !cache.IsEmpty() {
_ = cache.Scan(&token)
if token.Time+token.ExpiresIn*1000 > gtime.TimestampMilli() {
......@@ -69,7 +74,7 @@ func (s *client) getAccessToken(ctx context.Context) (err error) {
err = errors.New(jdRes.ResultMessage)
return
}
_, _ = g.Redis().Do("HMSET", append(g.Slice{CacheKey}, gutil.MapToSlice(gconv.Map(jdRes.Result))...)...)
_, _ = conn.Do("HMSET", append(g.Slice{CacheKey}, gutil.MapToSlice(gconv.Map(jdRes.Result))...)...)
s.AccessToken = jdRes.Result.AccessToken
return
}
......@@ -101,7 +106,12 @@ func (s *client) refreshToken(ctx context.Context, req *accessToken) (err error)
err = errors.New(jdRes.ResultMessage)
return
}
_, _ = g.Redis().Do("HMSET", append(g.Slice{CacheKey}, gutil.MapToSlice(gconv.Map(jdRes.Result))...)...)
conn := g.Redis().Ctx(ctx).Conn()
defer func() {
_ = conn.Close()
}()
_, _ = conn.DoVar("SELECT", 10)
_, _ = conn.Do("HMSET", append(g.Slice{CacheKey}, gutil.MapToSlice(gconv.Map(jdRes.Result))...)...)
s.AccessToken = jdRes.Result.AccessToken
return
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论