This commit is contained in:
2026-01-29 16:34:37 +08:00
parent f50e11a052
commit 5bff33547c
3 changed files with 51 additions and 32 deletions

View File

@@ -78,6 +78,14 @@ func (m *PDFCacheManager) GetCacheKeyByProduct(productID, version string) string
return hex.EncodeToString(hash[:])
}
// GetCacheKeyByReportID 生成缓存键基于报告ID
// 文件名格式MD5(report_id).pdf
// report_id 本身已经包含时间戳和随机数,所以 MD5 后就是唯一的
func (m *PDFCacheManager) GetCacheKeyByReportID(reportID string) string {
hash := md5.Sum([]byte(reportID))
return hex.EncodeToString(hash[:])
}
// GetCachePath 获取缓存文件路径
func (m *PDFCacheManager) GetCachePath(cacheKey string) string {
return filepath.Join(m.cacheDir, fmt.Sprintf("%s.pdf", cacheKey))
@@ -105,6 +113,20 @@ func (m *PDFCacheManager) GetByCacheKey(cacheKey string) ([]byte, bool, time.Tim
return m.getByKey(cacheKey, "", "")
}
// SetByReportID 将PDF文件保存到缓存基于报告ID
func (m *PDFCacheManager) SetByReportID(reportID string, pdfBytes []byte) error {
cacheKey := m.GetCacheKeyByReportID(reportID)
return m.setByKey(cacheKey, pdfBytes, reportID, "")
}
// GetByReportID 从缓存获取PDF文件基于报告ID
// 直接通过 report_id 的 MD5 计算文件名,无需遍历
// 返回PDF字节流、是否命中缓存、文件创建时间、错误
func (m *PDFCacheManager) GetByReportID(reportID string) ([]byte, bool, time.Time, error) {
cacheKey := m.GetCacheKeyByReportID(reportID)
return m.getByKey(cacheKey, reportID, "")
}
// getByKey 内部方法:根据缓存键获取文件
func (m *PDFCacheManager) getByKey(cacheKey string, key1, key2 string) ([]byte, bool, time.Time, error) {
cachePath := m.GetCachePath(cacheKey)