chore: 并入 iotlcm-openapi 历史到 openapi/(原提交 3b05d5a)
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
package bcache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/consts"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/library/cache"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-openapi/internal/library/mcache"
|
||||
"time"
|
||||
)
|
||||
|
||||
const apiAuthLocalBackfillTTL = 6 * time.Hour
|
||||
|
||||
// SetApiAuthToken 存储接口认证长令牌,先写本地内存缓存,再写共享缓存。
|
||||
func SetApiAuthToken(ctx context.Context, uuid string, token string, timeout time.Duration) (err error) {
|
||||
if uuid == "" {
|
||||
return errors.New("api auth token uuid invalid")
|
||||
}
|
||||
key := fmt.Sprintf(consts.KeyApiAuthToken, uuid)
|
||||
if err = mcache.Set(ctx, key, token, timeout); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = cache.Instance().Set(ctx, key, token, timeout); err != nil {
|
||||
_ = mcache.Remove(ctx, key)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetApiAuthToken 获取接口认证长令牌,优先读取本地内存缓存。
|
||||
func GetApiAuthToken(ctx context.Context, uuid string) (token string, err error) {
|
||||
if uuid == "" {
|
||||
return "", errors.New("api auth token uuid invalid")
|
||||
}
|
||||
key := fmt.Sprintf(consts.KeyApiAuthToken, uuid)
|
||||
t, err := mcache.Instance().Get(ctx, key)
|
||||
if err == nil && !t.IsNil() {
|
||||
return t.String(), nil
|
||||
}
|
||||
|
||||
t, err = cache.Instance().Get(ctx, key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !t.IsNil() {
|
||||
_ = mcache.Set(ctx, key, t.String(), apiAuthLocalBackfillTTL)
|
||||
return t.String(), nil
|
||||
}
|
||||
return "", errors.New("api auth token not found")
|
||||
}
|
||||
|
||||
// DelApiAuthToken 删除接口认证长令牌,先删本地内存缓存,再删共享缓存。
|
||||
func DelApiAuthToken(ctx context.Context, uuid string) (err error) {
|
||||
if uuid == "" {
|
||||
return errors.New("api auth token uuid invalid")
|
||||
}
|
||||
key := fmt.Sprintf(consts.KeyApiAuthToken, uuid)
|
||||
if err = mcache.Remove(ctx, key); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = cache.Instance().Remove(ctx, key)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetApiAuthActiveToken 设置机构维度的当前有效令牌索引。
|
||||
func SetApiAuthActiveToken(ctx context.Context, deptId int64, uuid string, timeout time.Duration) (err error) {
|
||||
if deptId <= 0 || uuid == "" {
|
||||
return errors.New("api auth active token key invalid")
|
||||
}
|
||||
key := fmt.Sprintf(consts.KeyApiAuthActiveToken, deptId)
|
||||
if err = mcache.Set(ctx, key, uuid, timeout); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = cache.Instance().Set(ctx, key, uuid, timeout); err != nil {
|
||||
_ = mcache.Remove(ctx, key)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetApiAuthActiveToken 获取机构维度的当前有效令牌索引。
|
||||
func GetApiAuthActiveToken(ctx context.Context, deptId int64) (uuid string, err error) {
|
||||
if deptId <= 0 {
|
||||
return "", errors.New("api auth active token key invalid")
|
||||
}
|
||||
key := fmt.Sprintf(consts.KeyApiAuthActiveToken, deptId)
|
||||
t, err := mcache.Instance().Get(ctx, key)
|
||||
if err == nil && !t.IsNil() {
|
||||
return t.String(), nil
|
||||
}
|
||||
|
||||
t, err = cache.Instance().Get(ctx, key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !t.IsNil() {
|
||||
_ = mcache.Set(ctx, key, t.String(), apiAuthLocalBackfillTTL)
|
||||
return t.String(), nil
|
||||
}
|
||||
return "", errors.New("api auth active token not found")
|
||||
}
|
||||
|
||||
// DelApiAuthActiveToken 删除机构维度的当前有效令牌索引。
|
||||
func DelApiAuthActiveToken(ctx context.Context, deptId int64) (err error) {
|
||||
if deptId <= 0 {
|
||||
return errors.New("api auth active token key invalid")
|
||||
}
|
||||
key := fmt.Sprintf(consts.KeyApiAuthActiveToken, deptId)
|
||||
if err = mcache.Remove(ctx, key); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = cache.Instance().Remove(ctx, key)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user