From 2971c5f52dceb5ad45536099a26f28c0e0f85d94 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Mon, 21 Apr 2025 14:02:56 +0800 Subject: [PATCH] =?UTF-8?q?s3=20=E6=B7=BB=E5=8A=A0=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=BB=B6=E8=BF=9F=E7=88=86=E7=82=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fs/s3_adapter.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/fs/s3_adapter.go b/fs/s3_adapter.go index 8021db1..b455dd3 100644 --- a/fs/s3_adapter.go +++ b/fs/s3_adapter.go @@ -10,6 +10,7 @@ import ( "log" "path" "sort" + "sync" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -21,6 +22,7 @@ import ( type S3Adapter struct { StorageConfig config.StorageConfig s3Client *s3.Client + cache sync.Map } func (s *S3Adapter) getClient() (*s3.Client, error) { @@ -49,6 +51,15 @@ func (s *S3Adapter) GetFileList(ctx context.Context, dirPath string, relDt time. _, span := tracer.Start(ctx, "GetFileList_s3") defer span.End() + key := fmt.Sprintf("%s_%s", dirPath, relDt.Format("2006-01-02")) + 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存储桶") @@ -132,5 +143,17 @@ func (s *S3Adapter) GetFileList(ctx context.Context, dirPath string, relDt time. return fileList[i].StartTime.Before(fileList[j].StartTime) }) span.SetStatus(codes.Ok, "文件读取成功") + + cacheItem := cacheItem{ + data: fileList, + expires: time.Now().Add(10 * time.Second), + } + s.cache.Store(key, cacheItem) + return fileList, nil } + +type cacheItem struct { + data []dto.File + expires time.Time +}