f
This commit is contained in:
@@ -2,6 +2,8 @@ package pdfg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -94,9 +96,6 @@ func ProcessPDFG01GZRequest(ctx context.Context, params []byte, deps *processors
|
||||
pdfGenService = pdfgen.NewPDFGenService(defaultCfg, zapLogger)
|
||||
}
|
||||
|
||||
// 计算缓存键(内部使用,不直接暴露给用户)
|
||||
cacheKey := cacheManager.GetCacheKey(paramsDto.Name, paramsDto.IDCard)
|
||||
|
||||
// 直接生成PDF,不检查缓存(每次都重新生成)
|
||||
zapLogger.Info("开始生成PDF",
|
||||
zap.String("name", paramsDto.Name),
|
||||
@@ -153,15 +152,15 @@ func ProcessPDFG01GZRequest(ctx context.Context, params []byte, deps *processors
|
||||
return nil, errors.Join(processors.ErrSystem, fmt.Errorf("生成PDF失败: %w", err))
|
||||
}
|
||||
|
||||
// 保存到缓存(基于姓名+身份证)
|
||||
if err := cacheManager.Set(paramsDto.Name, paramsDto.IDCard, pdfResp.PDFBytes); err != nil {
|
||||
// 生成报告ID(每次请求都生成唯一的ID)
|
||||
reportID := generateReportID()
|
||||
|
||||
// 保存到缓存(基于报告ID,文件名包含时间戳确保唯一性)
|
||||
if err := cacheManager.SetByReportID(reportID, pdfResp.PDFBytes); err != nil {
|
||||
zapLogger.Warn("保存PDF到缓存失败", zap.Error(err))
|
||||
// 不影响返回结果,只记录警告
|
||||
}
|
||||
|
||||
// 生成报告ID(与内部缓存键解耦,对外只暴露带前缀的ID)
|
||||
reportID := generateReportID(cacheKey)
|
||||
|
||||
// 生成下载链接(基于报告ID)
|
||||
downloadURL := generateDownloadURL(apiDomain, reportID)
|
||||
expiresAt := time.Now().Add(cacheTTL)
|
||||
@@ -473,10 +472,18 @@ func generateReportNumber() string {
|
||||
}
|
||||
|
||||
// generateReportID 生成对外可见的报告ID
|
||||
// 结构:{report_number}-{cacheKey},前缀变化以避免用户轻易看出是否命中缓存
|
||||
func generateReportID(cacheKey string) string {
|
||||
// 每次请求都生成唯一的ID,格式:{report_number}-{随机字符串}
|
||||
// 注意:不再包含cacheKey,因为每次请求都会重新生成,不需要通过ID定位缓存文件
|
||||
func generateReportID() string {
|
||||
reportNumber := generateReportNumber()
|
||||
return fmt.Sprintf("%s-%s", reportNumber, cacheKey)
|
||||
// 生成8字节随机字符串,确保每次请求ID都不同
|
||||
randomBytes := make([]byte, 8)
|
||||
if _, err := rand.Read(randomBytes); err != nil {
|
||||
// 如果随机数生成失败,使用纳秒时间戳作为后备
|
||||
randomBytes = []byte(fmt.Sprintf("%d", time.Now().UnixNano()))
|
||||
}
|
||||
randomStr := hex.EncodeToString(randomBytes)
|
||||
return fmt.Sprintf("%s-%s", reportNumber, randomStr)
|
||||
}
|
||||
|
||||
// generateDownloadURL 生成下载链接(基于报告ID/缓存键)
|
||||
|
||||
Reference in New Issue
Block a user