s3 修复缓存键避免被逻辑修改

This commit is contained in:
Jerry Yan 2025-04-21 14:44:23 +08:00
parent e6f93a4d37
commit 509b829c5b

View File

@ -53,8 +53,8 @@ func (s *S3Adapter) GetFileList(ctx context.Context, dirPath string, relDt time.
span.SetAttributes(attribute.String("path", dirPath)) span.SetAttributes(attribute.String("path", dirPath))
span.SetAttributes(attribute.String("relativeDate", relDt.Format("2006-01-02"))) span.SetAttributes(attribute.String("relativeDate", relDt.Format("2006-01-02")))
key := fmt.Sprintf("%s_%s", dirPath, relDt.Format("2006-01-02")) cacheKey := fmt.Sprintf("%s_%s", dirPath, relDt.Format("2006-01-02"))
if cachedInterface, ok := s.cache.Load(key); ok { if cachedInterface, ok := s.cache.Load(cacheKey); ok {
cachedItem := cachedInterface.(cacheItem) cachedItem := cachedInterface.(cacheItem)
if time.Now().Before(cachedItem.expires) { if time.Now().Before(cachedItem.expires) {
span.SetAttributes(attribute.Bool("cache.hit", true)) span.SetAttributes(attribute.Bool("cache.hit", true))
@ -62,7 +62,7 @@ func (s *S3Adapter) GetFileList(ctx context.Context, dirPath string, relDt time.
} }
} }
mutexKey := fmt.Sprintf("lock_%s", key) mutexKey := fmt.Sprintf("lock_%s", cacheKey)
mutex, _ := s.cache.LoadOrStore(mutexKey, &sync.Mutex{}) mutex, _ := s.cache.LoadOrStore(mutexKey, &sync.Mutex{})
lock := mutex.(*sync.Mutex) lock := mutex.(*sync.Mutex)
defer func() { defer func() {
@ -72,7 +72,7 @@ func (s *S3Adapter) GetFileList(ctx context.Context, dirPath string, relDt time.
}() }()
lock.Lock() lock.Lock()
if cachedInterface, ok := s.cache.Load(key); ok { if cachedInterface, ok := s.cache.Load(cacheKey); ok {
cachedItem := cachedInterface.(cacheItem) cachedItem := cachedInterface.(cacheItem)
if time.Now().Before(cachedItem.expires) { if time.Now().Before(cachedItem.expires) {
span.SetAttributes(attribute.Bool("cache.hit", true)) span.SetAttributes(attribute.Bool("cache.hit", true))
@ -168,7 +168,7 @@ func (s *S3Adapter) GetFileList(ctx context.Context, dirPath string, relDt time.
data: fileList, data: fileList,
expires: time.Now().Add(10 * time.Second), expires: time.Now().Add(10 * time.Second),
} }
s.cache.Store(key, cacheItem) s.cache.Store(cacheKey, cacheItem)
return fileList, nil return fileList, nil
} }