2929import org .commonjava .indy .model .core .StoreKey ;
3030import org .commonjava .indy .model .core .StoreType ;
3131import org .commonjava .indy .model .core .io .IndyObjectMapper ;
32+ import org .commonjava .indy .subsys .infinispan .BasicCacheHandle ;
3233import org .commonjava .indy .subsys .infinispan .CacheHandle ;
34+ import org .commonjava .indy .subsys .infinispan .CacheProducer ;
3335import org .commonjava .o11yphant .metrics .annotation .Measure ;
3436import org .slf4j .Logger ;
3537import org .slf4j .LoggerFactory ;
4345import java .util .List ;
4446import java .util .Map ;
4547import java .util .Set ;
48+ import java .util .concurrent .TimeUnit ;
4649import java .util .function .Consumer ;
4750import java .util .stream .Collectors ;
4851import 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}
0 commit comments