Skip to content

Commit 3486854

Browse files
committed
fix cache fall through
1 parent 2cb0031 commit 3486854

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

server/server.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,40 @@ func getFromCache(ctx context.Context, version, path string) *CacheEntry {
165165
cacheMutex.RUnlock()
166166
}
167167

168+
// Before hitting S3, check if parent directory is cached
169+
// If so, we can determine if this file exists without S3
170+
dirPath := filepath.Dir(strings.TrimPrefix(path, "/"))
171+
if dirPath != "." && dirPath != "" {
172+
dirCacheKey := fmt.Sprintf("%s%s", version, dirPath)
173+
cacheMutex.RLock()
174+
if dirEntry, ok := cache.Get(dirCacheKey); ok && time.Now().Before(dirEntry.ExpiresAt) {
175+
cacheMutex.RUnlock()
176+
if dirEntry.Exists && !dirEntry.IsFile {
177+
// Parent directory is cached, check if file is in the list
178+
filename := filepath.Base(path)
179+
for _, f := range dirEntry.Files {
180+
if f == filename {
181+
// File exists in parent directory, fetch it
182+
goto fetchFile
183+
}
184+
}
185+
// File not in parent directory's file list, so it doesn't exist
186+
s3CacheHits.Inc() // This is still a cache hit - we used cached dir info
187+
entry := &CacheEntry{
188+
Exists: false,
189+
ExpiresAt: time.Now().Add(cacheTTLNotFound),
190+
}
191+
cacheMutex.Lock()
192+
cache.Add(cacheKey, entry)
193+
cacheMutex.Unlock()
194+
return entry
195+
}
196+
} else {
197+
cacheMutex.RUnlock()
198+
}
199+
}
200+
201+
fetchFile:
168202
s3CacheMisses.Inc()
169203

170204
// Try to fetch as a file first

0 commit comments

Comments
 (0)