diff --git a/fs/local_adapter.go b/fs/local_adapter.go index dbef8af..80e4291 100644 --- a/fs/local_adapter.go +++ b/fs/local_adapter.go @@ -26,6 +26,9 @@ func (l *LocalAdapter) GetFileList(ctx context.Context, dirPath string, relDt ti span.SetStatus(codes.Error, "未配置存储路径") return nil, fmt.Errorf("未配置存储路径") } + + span.SetAttributes(attribute.String("path", dirPath)) + span.SetAttributes(attribute.String("relativeDate", relDt.Format("2006-01-02"))) // 读取文件夹下目录 files, err := os.ReadDir(path.Join(l.StorageConfig.Path, dirPath)) if err != nil { diff --git a/fs/s3_adapter.go b/fs/s3_adapter.go index b455dd3..f7455d8 100644 --- a/fs/s3_adapter.go +++ b/fs/s3_adapter.go @@ -51,6 +51,8 @@ func (s *S3Adapter) GetFileList(ctx context.Context, dirPath string, relDt time. _, span := tracer.Start(ctx, "GetFileList_s3") defer span.End() + span.SetAttributes(attribute.String("path", dirPath)) + span.SetAttributes(attribute.String("relativeDate", relDt.Format("2006-01-02"))) key := fmt.Sprintf("%s_%s", dirPath, relDt.Format("2006-01-02")) if cachedInterface, ok := s.cache.Load(key); ok { cachedItem := cachedInterface.(cacheItem) @@ -60,6 +62,24 @@ func (s *S3Adapter) GetFileList(ctx context.Context, dirPath string, relDt time. } } + mutexKey := fmt.Sprintf("lock_%s", key) + mutex, _ := s.cache.LoadOrStore(mutexKey, &sync.Mutex{}) + lock := mutex.(*sync.Mutex) + defer func() { + // 解锁后删除锁(避免内存泄漏) + s.cache.Delete(mutexKey) + lock.Unlock() + }() + lock.Lock() + + if cachedInterface, ok := s.cache.Load(key); ok { + cachedItem := cachedInterface.(cacheItem) + if time.Now().Before(cachedItem.expires) { + span.SetAttributes(attribute.Bool("cache.hit", true)) + return cachedItem.data, nil + } + } + if s.StorageConfig.S3.Bucket == "" { span.SetAttributes(attribute.String("error", "未配置S3存储桶")) span.SetStatus(codes.Error, "未配置S3存储桶")