1818import com .fasterxml .jackson .databind .ObjectMapper ;
1919import io .quarkus .runtime .Startup ;
2020import org .apache .commons .io .IOUtils ;
21+ import org .commonjava .service .storage .config .StorageServiceConfig ;
2122import org .commonjava .service .storage .dto .*;
2223import org .commonjava .storage .pathmapped .core .PathMappedFileManager ;
2324import org .commonjava .storage .pathmapped .model .FileChecksum ;
@@ -61,10 +62,16 @@ public class StorageController
6162 @ Inject
6263 PathMappedFileManager fileManager ;
6364
65+ @ Inject
66+ StorageServiceConfig config ;
67+
6468 private ObjectMapper objectMapper = new ObjectMapper ();
6569
6670 private static final int DEFAULT_RECURSIVE_LIST_LIMIT = 5000 ;
6771
72+ // Cache for filesystems list
73+ private volatile CachedFilesystems cachedFilesystems ;
74+
6875 public InputStream openInputStream ( String fileSystem , String path ) throws IOException
6976 {
7077 return fileManager .openInputStream ( fileSystem , path );
@@ -248,16 +255,36 @@ public BatchDeleteResult purgeFilesystem(String filesystem)
248255 {
249256 fileManager .purgeFilesystem ( statistics );
250257 }
258+ invalidateFilesystemsCache ();
251259 return ret ;
252260 }
253261
254262 public Collection <String > getFilesystems ()
255263 {
264+ CachedFilesystems cached = cachedFilesystems ;
265+ long now = System .currentTimeMillis ();
266+
267+ // Check if cache is valid
268+ if ( cached != null && ( now - cached .timestamp ) < TimeUnit .SECONDS .toMillis ( config .filesystemsCacheTtlSeconds () ) )
269+ {
270+ logger .debug ( "Returning cached filesystems list (age: {}ms)" , now - cached .timestamp );
271+ return cached .filesystems ;
272+ }
273+
274+ // Cache miss or expired - fetch from database
275+ logger .debug ( "Cache miss or expired, fetching filesystems from database" );
256276 Collection <? extends Filesystem > filesystems = fileManager .getFilesystems ();
277+ Collection <String > result ;
257278 if ( filesystems != null ) {
258- return filesystems .stream ().map (filesystem -> filesystem .getFilesystem ()).sorted ().collect (Collectors .toList ());
279+ result = filesystems .stream ().map (filesystem -> filesystem .getFilesystem ()).sorted ().collect (Collectors .toList ());
280+ } else {
281+ result = emptyList ();
259282 }
260- return emptyList ();
283+
284+ // Update cache
285+ cachedFilesystems = new CachedFilesystems ( result , now );
286+ logger .debug ( "Cached filesystems list (size: {})" , result .size () );
287+ return result ;
261288 }
262289
263290 public Collection <? extends Filesystem > getEmptyFilesystems ()
@@ -271,6 +298,7 @@ public void purgeEmptyFilesystems()
271298 {
272299 Collection <? extends Filesystem > ret = getEmptyFilesystems ();
273300 ret .forEach ( filesystem -> fileManager .purgeFilesystem ( filesystem ));
301+ invalidateFilesystemsCache ();
274302 }
275303
276304 /**
@@ -426,4 +454,28 @@ public FileCopyResult copy(FileCopyRequest request)
426454
427455 return new FileCopyResult ( true , completed , skipped );
428456 }
457+
458+ /**
459+ * Invalidates the filesystems cache. Should be called whenever filesystems are added or removed.
460+ */
461+ private void invalidateFilesystemsCache ()
462+ {
463+ logger .debug ( "Invalidating filesystems cache" );
464+ cachedFilesystems = null ;
465+ }
466+
467+ /**
468+ * Simple cache entry for filesystems list.
469+ */
470+ private static class CachedFilesystems
471+ {
472+ final Collection <String > filesystems ;
473+ final long timestamp ;
474+
475+ CachedFilesystems ( Collection <String > filesystems , long timestamp )
476+ {
477+ this .filesystems = filesystems ;
478+ this .timestamp = timestamp ;
479+ }
480+ }
429481}
0 commit comments