From e6f93a4d37687fce11ae0d5c325173a7518ca390 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Mon, 21 Apr 2025 14:37:51 +0800 Subject: [PATCH] =?UTF-8?q?s3=20=E9=81=BF=E5=85=8D=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E5=87=BB=E7=A9=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fs/local_adapter.go | 3 +++ fs/s3_adapter.go | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) 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存储桶")