Skip to content

Commit 087ba48

Browse files
committed
Cache filesystems
1 parent 721bb3d commit 087ba48

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

src/main/java/org/commonjava/service/storage/config/StorageServiceConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ public interface StorageServiceConfig
6464
@WithName( "bucket.name" )
6565
@WithDefault( "test" )
6666
String bucketName();
67+
68+
@WithName( "filesystemsCacheTtlSeconds" )
69+
@WithDefault( "600" )
70+
int filesystemsCacheTtlSeconds();
6771
}

src/main/java/org/commonjava/service/storage/controller/StorageController.java

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.fasterxml.jackson.databind.ObjectMapper;
1919
import io.quarkus.runtime.Startup;
2020
import org.apache.commons.io.IOUtils;
21+
import org.commonjava.service.storage.config.StorageServiceConfig;
2122
import org.commonjava.service.storage.dto.*;
2223
import org.commonjava.storage.pathmapped.core.PathMappedFileManager;
2324
import 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
}

src/main/resources/application.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ storage:
6868
readonly: false
6969
deduplicatePattern: "(generic-http|npm).+"
7070
removableFilesystemPattern: ".+:(remote|group):.+"
71+
filesystemsCacheTtlSeconds: 600
7172
#type: s3
7273
#bucket:
7374
# name: test

0 commit comments

Comments
 (0)