Skip to content

Commit b394e61

Browse files
committed
Add in-memory cache for artifact store
1 parent da24a51 commit b394e61

2 files changed

Lines changed: 50 additions & 6 deletions

File tree

db/cassandra/src/main/java/org/commonjava/indy/cassandra/data/CassandraStoreDataManager.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
import org.commonjava.indy.model.core.StoreKey;
3030
import org.commonjava.indy.model.core.StoreType;
3131
import org.commonjava.indy.model.core.io.IndyObjectMapper;
32+
import org.commonjava.indy.subsys.infinispan.BasicCacheHandle;
3233
import org.commonjava.indy.subsys.infinispan.CacheHandle;
34+
import org.commonjava.indy.subsys.infinispan.CacheProducer;
3335
import org.commonjava.o11yphant.metrics.annotation.Measure;
3436
import org.slf4j.Logger;
3537
import org.slf4j.LoggerFactory;
@@ -43,6 +45,7 @@
4345
import java.util.List;
4446
import java.util.Map;
4547
import java.util.Set;
48+
import java.util.concurrent.TimeUnit;
4649
import java.util.function.Consumer;
4750
import java.util.stream.Collectors;
4851
import java.util.stream.Stream;
@@ -64,18 +67,26 @@ public class CassandraStoreDataManager extends AbstractStoreDataManager
6467
@Inject
6568
IndyObjectMapper objectMapper;
6669

70+
@Inject
71+
private CacheProducer cacheProducer;
72+
6773
@Inject
6874
@RemoteKojiStoreDataCache
6975
private CacheHandle<StoreKey, ArtifactStore> remoteKojiStores;
7076

77+
private final String ARTIFACT_STORE = "artifact-store";
78+
79+
private final Integer STORE_EXPIRATION_IN_MINS = 15;
80+
7181
protected CassandraStoreDataManager()
7282
{
7383
}
7484

75-
CassandraStoreDataManager( final CassandraStoreQuery storeQuery, final IndyObjectMapper objectMapper )
85+
CassandraStoreDataManager( final CassandraStoreQuery storeQuery, final IndyObjectMapper objectMapper, final CacheProducer cacheProducer )
7686
{
7787
this.storeQuery = storeQuery;
7888
this.objectMapper = objectMapper;
89+
this.cacheProducer = cacheProducer;
7990
}
8091

8192
@Override
@@ -99,9 +110,7 @@ protected ArtifactStore getArtifactStoreInternal( StoreKey key )
99110
}
100111
}
101112

102-
DtxArtifactStore dtxArtifactStore = storeQuery.getArtifactStore( key.getPackageType(), key.getType(), key.getName() );
103-
104-
return toArtifactStore( dtxArtifactStore );
113+
return computeIfAbsent( ARTIFACT_STORE, key, STORE_EXPIRATION_IN_MINS, Boolean.FALSE );
105114
}
106115

107116
@Override
@@ -215,7 +224,7 @@ protected ArtifactStore putArtifactStoreInternal( StoreKey storeKey, ArtifactSto
215224
DtxArtifactStore dtxArtifactStore = toDtxArtifactStore( storeKey, store );
216225
storeQuery.createDtxArtifactStore( dtxArtifactStore );
217226

218-
return toArtifactStore( dtxArtifactStore );
227+
return computeIfAbsent( ARTIFACT_STORE, storeKey, STORE_EXPIRATION_IN_MINS, Boolean.TRUE );
219228
}
220229

221230
@Override
@@ -578,4 +587,31 @@ public void initRemoteStoresCache()
578587
|| "koji-binary".equals(
579588
s.getMetadata(ArtifactStore.METADATA_ORIGIN ) ) ) ).forEach( s -> remoteKojiStores.put( s.getKey(), s ) );
580589
}
590+
591+
private ArtifactStore computeIfAbsent( String name, StoreKey key, int expirationMins, boolean forceQuery )
592+
{
593+
logger.debug( "computeIfAbsent, cache: {}, key: {}", name, key );
594+
595+
BasicCacheHandle<StoreKey, ArtifactStore> cache = cacheProducer.getBasicCache( name );
596+
ArtifactStore store = cache.get( key );
597+
if ( store == null || forceQuery )
598+
{
599+
logger.trace( "Entry not found, run put, expirationMins: {}", expirationMins );
600+
601+
DtxArtifactStore dtxArtifactStore = storeQuery.getArtifactStore( key.getPackageType(), key.getType(), key.getName() );
602+
603+
store = toArtifactStore( dtxArtifactStore );
604+
if ( expirationMins > 0 )
605+
{
606+
cache.put( key, store, expirationMins, TimeUnit.MINUTES );
607+
}
608+
else
609+
{
610+
cache.put( key, store );
611+
}
612+
}
613+
614+
logger.trace( "Return value, cache: {}, key: {}, ret: {}", name, key, store );
615+
return store;
616+
}
581617
}

db/cassandra/src/test/java/org/commonjava/indy/cassandra/data/CassandraTCKFixtureProvider.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import org.commonjava.indy.model.core.io.IndyObjectMapper;
2424
import org.commonjava.indy.subsys.cassandra.CassandraClient;
2525
import org.commonjava.indy.subsys.cassandra.config.CassandraConfig;
26+
import org.commonjava.indy.subsys.infinispan.CacheProducer;
27+
import org.infinispan.configuration.cache.ConfigurationBuilder;
28+
import org.infinispan.manager.DefaultCacheManager;
2629

2730
public class CassandraTCKFixtureProvider
2831
implements TCKFixtureProvider
@@ -31,6 +34,8 @@ public class CassandraTCKFixtureProvider
3134

3235
private static CassandraClient client;
3336

37+
private CacheProducer cacheProducer;
38+
3439
protected void init()
3540
throws Exception
3641
{
@@ -49,8 +54,11 @@ protected void init()
4954

5055
client = new CassandraClient( config );
5156

57+
DefaultCacheManager cacheManager =
58+
new DefaultCacheManager( new ConfigurationBuilder().simpleCache( true ).build() );
59+
cacheProducer = new CacheProducer( null, cacheManager, null );
5260
CassandraStoreQuery storeQuery = new CassandraStoreQuery( client, storeConfig, indyConfig );
53-
dataManager = new CassandraStoreDataManager( storeQuery, new IndyObjectMapper( true ) );
61+
dataManager = new CassandraStoreDataManager( storeQuery, new IndyObjectMapper( true ), cacheProducer );
5462
}
5563

5664
@Override

0 commit comments

Comments
 (0)