feat(task): 优化文件列表获取逻辑并添加缓存机制

- 实现按时间前缀获取文件列表,支持小时级目录检索
- 添加降级机制,当时间前缀方式无法找到文件时回退到按天目录
- 在适配器层添加单例模式和客户端连接池管理
- 为S3和AliOSS适配器添加文件列表缓存功能
- 修复跨天任务处理逻辑,约束业务不支持跨天操作
- 优化文件去重逻辑,避免重复处理相同文件
- 添加详细的链路追踪和错误处理机制
This commit is contained in:
2025-12-29 18:39:24 +08:00
parent 686401162f
commit 10e39a506c
5 changed files with 178 additions and 47 deletions

View File

@@ -9,6 +9,7 @@ import (
"fmt"
"path"
"sort"
"sync"
"time"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
@@ -18,25 +19,44 @@ import (
)
type AliOSSAdapter struct {
StorageConfig config.StorageConfig
ossClient *oss.Client
StorageConfig config.StorageConfig
fileListCacheOnce sync.Once
fileListCache *fileListCache
clientOnce sync.Once
clientErr error
ossClient *oss.Client
}
func (a *AliOSSAdapter) getClient() (*oss.Client, error) {
if a.ossClient == nil {
a.clientOnce.Do(func() {
client, err := oss.New(
a.StorageConfig.AliOSS.Endpoint,
a.StorageConfig.AliOSS.AccessKeyId,
a.StorageConfig.AliOSS.AccessKeySecret,
)
if err != nil {
return nil, fmt.Errorf("创建阿里云OSS客户端失败: %w", err)
a.clientErr = fmt.Errorf("创建阿里云OSS客户端失败: %w", err)
return
}
a.ossClient = client
})
if a.clientErr != nil {
return nil, a.clientErr
}
if a.ossClient == nil {
return nil, fmt.Errorf("阿里云OSS客户端未初始化")
}
return a.ossClient, nil
}
func (a *AliOSSAdapter) getFileListCache() *fileListCache {
a.fileListCacheOnce.Do(func() {
a.fileListCache = newFileListCache(getFileListCacheTTL(), getFileListCacheMaxEntries())
})
return a.fileListCache
}
func (a *AliOSSAdapter) GetFileList(ctx context.Context, dirPath string, relDt time.Time) ([]dto.File, error) {
_, span := tracer.Start(ctx, "GetFileList_alioss")
defer span.End()
@@ -50,7 +70,7 @@ func (a *AliOSSAdapter) GetFileList(ctx context.Context, dirPath string, relDt t
}
cacheKey := fmt.Sprintf("%s_%s", dirPath, relDt.Format("2006-01-02"))
fileListCache := getAliOssFileListCache()
fileListCache := a.getFileListCache()
if cachedFiles, ok := fileListCache.Get(cacheKey); ok {
logger.Debug("获取已缓存列表", zap.String("cacheKey", cacheKey))
span.SetAttributes(attribute.Bool("cache.hit", true))