s3 避免缓存击穿
This commit is contained in:
parent
2971c5f52d
commit
e6f93a4d37
@ -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 {
|
||||
|
@ -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存储桶")
|
||||
|
Loading…
x
Reference in New Issue
Block a user