temp
This commit is contained in:
@@ -2,9 +2,12 @@ package query
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"tyc-server/app/main/api/internal/service"
|
||||
"tyc-server/common/ctxdata"
|
||||
@@ -140,13 +143,13 @@ func (l *QueryServiceLogic) CacheData(params map[string]interface{}, Product str
|
||||
if marshalErr != nil {
|
||||
return "", errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "查询服务, 序列化参数失败: %+v", marshalErr)
|
||||
}
|
||||
outTradeNo := l.svcCtx.AlipayService.GenerateOutTradeNo()
|
||||
redisKey := fmt.Sprintf("%d:%s", userID, outTradeNo)
|
||||
cacheNo := l.GenerateCacheNo()
|
||||
redisKey := fmt.Sprintf("%d:%s", userID, cacheNo)
|
||||
cacheErr := l.svcCtx.Redis.SetexCtx(l.ctx, redisKey, string(jsonData), int(2*time.Hour))
|
||||
if cacheErr != nil {
|
||||
return "", cacheErr
|
||||
}
|
||||
return outTradeNo, nil
|
||||
return cacheNo, nil
|
||||
}
|
||||
|
||||
var productProcessors = map[string]func(*QueryServiceLogic, *types.QueryServiceReq) (*types.QueryServiceResp, error){
|
||||
@@ -2067,3 +2070,27 @@ func (l *QueryServiceLogic) ProcessTocPersonEnterpriseProLogic(req *types.QueryS
|
||||
return &types.QueryServiceResp{Id: cacheNo}, nil
|
||||
|
||||
}
|
||||
|
||||
// 全局原子计数器
|
||||
var CacheNoCounter uint32 = 0
|
||||
|
||||
func (l *QueryServiceLogic) GenerateCacheNo() string {
|
||||
// 获取当前时间戳(秒级)
|
||||
timestamp := time.Now().Unix()
|
||||
timeStr := strconv.FormatInt(timestamp, 10)
|
||||
|
||||
// 原子递增计数器
|
||||
counter := atomic.AddUint32(&CacheNoCounter, 1)
|
||||
|
||||
// 生成4字节真随机数
|
||||
randomBytes := make([]byte, 4)
|
||||
_, err := rand.Read(randomBytes)
|
||||
if err != nil {
|
||||
// 如果随机数生成失败,回退到使用时间纳秒数据
|
||||
randomBytes = []byte(strconv.FormatInt(time.Now().UnixNano()%1000000, 16))
|
||||
}
|
||||
randomHex := hex.EncodeToString(randomBytes)
|
||||
|
||||
// 组合所有部分: 前缀 + 时间戳 + 计数器 + 随机数
|
||||
return fmt.Sprintf("query_%s%06x%s", timeStr, counter%0xFFFFFF, randomHex[:6])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user