f
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user