s3 添加缓存避免延迟爆炸
This commit is contained in:
parent
3d7c88de5f
commit
2971c5f52d
@ -10,6 +10,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"path"
|
"path"
|
||||||
"sort"
|
"sort"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go-v2/aws"
|
"github.com/aws/aws-sdk-go-v2/aws"
|
||||||
@ -21,6 +22,7 @@ import (
|
|||||||
type S3Adapter struct {
|
type S3Adapter struct {
|
||||||
StorageConfig config.StorageConfig
|
StorageConfig config.StorageConfig
|
||||||
s3Client *s3.Client
|
s3Client *s3.Client
|
||||||
|
cache sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *S3Adapter) getClient() (*s3.Client, error) {
|
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")
|
_, span := tracer.Start(ctx, "GetFileList_s3")
|
||||||
defer span.End()
|
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 == "" {
|
if s.StorageConfig.S3.Bucket == "" {
|
||||||
span.SetAttributes(attribute.String("error", "未配置S3存储桶"))
|
span.SetAttributes(attribute.String("error", "未配置S3存储桶"))
|
||||||
span.SetStatus(codes.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)
|
return fileList[i].StartTime.Before(fileList[j].StartTime)
|
||||||
})
|
})
|
||||||
span.SetStatus(codes.Ok, "文件读取成功")
|
span.SetStatus(codes.Ok, "文件读取成功")
|
||||||
|
|
||||||
|
cacheItem := cacheItem{
|
||||||
|
data: fileList,
|
||||||
|
expires: time.Now().Add(10 * time.Second),
|
||||||
|
}
|
||||||
|
s.cache.Store(key, cacheItem)
|
||||||
|
|
||||||
return fileList, nil
|
return fileList, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type cacheItem struct {
|
||||||
|
data []dto.File
|
||||||
|
expires time.Time
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user