-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: cache recommendations Signed-off-by: Ruben Romero Montes <[email protected]> * feat: cache recommendations Signed-off-by: Ruben Romero Montes <[email protected]> * feat: cache recommendations Signed-off-by: Ruben Romero Montes <[email protected]> --------- Signed-off-by: Ruben Romero Montes <[email protected]>
- Loading branch information
Showing
19 changed files
with
601 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/redhat/exhort/integration/cache/CacheService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.redhat.exhort.integration.cache; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.apache.camel.Body; | ||
import org.apache.camel.ExchangeProperty; | ||
|
||
import com.redhat.exhort.api.PackageRef; | ||
import com.redhat.exhort.integration.Constants; | ||
import com.redhat.exhort.model.trustedcontent.CachedRecommendation; | ||
import com.redhat.exhort.model.trustedcontent.TrustedContentResponse; | ||
|
||
public interface CacheService { | ||
|
||
public void cacheRecommendations( | ||
@Body TrustedContentResponse response, | ||
@ExchangeProperty(Constants.CACHED_RECOMMENDATIONS) Set<PackageRef> misses); | ||
|
||
public Map<PackageRef, CachedRecommendation> getRecommendations(Set<PackageRef> purls); | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/com/redhat/exhort/integration/cache/RedisCacheService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
* and other contributors as indicated by the @author tags. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.redhat.exhort.integration.cache; | ||
|
||
import java.time.Duration; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
||
import com.redhat.exhort.api.PackageRef; | ||
import com.redhat.exhort.model.trustedcontent.CachedRecommendation; | ||
import com.redhat.exhort.model.trustedcontent.TrustedContentResponse; | ||
|
||
import io.quarkus.redis.datasource.RedisDataSource; | ||
import io.quarkus.redis.datasource.value.ValueCommands; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
@ApplicationScoped | ||
public class RedisCacheService implements CacheService { | ||
|
||
@ConfigProperty(name = "recommendations.cache.ttl", defaultValue = "1d") | ||
Duration recommendationTtl; | ||
|
||
private final ValueCommands<String, CachedRecommendation> recommendationsCommands; | ||
|
||
public RedisCacheService(RedisDataSource ds) { | ||
this.recommendationsCommands = ds.value(CachedRecommendation.class); | ||
} | ||
|
||
@Override | ||
public void cacheRecommendations(TrustedContentResponse response, Set<PackageRef> misses) { | ||
if (response == null || response.status() == null || !response.status().getOk()) { | ||
return; | ||
} | ||
misses.stream() | ||
.forEach( | ||
v -> | ||
recommendationsCommands.psetex( | ||
"recommendations:" + v.ref(), | ||
recommendationTtl.toMillis(), | ||
new CachedRecommendation(v, response.recommendations().get(v)))); | ||
} | ||
|
||
@Override | ||
public Map<PackageRef, CachedRecommendation> getRecommendations(Set<PackageRef> purls) { | ||
if (purls == null || purls.isEmpty()) { | ||
return Collections.emptyMap(); | ||
} | ||
var result = | ||
recommendationsCommands.mget( | ||
purls.stream().map(p -> "recommendations:" + p.ref()).toArray(String[]::new)); | ||
return result.values().stream() | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toMap(v -> v.ref(), v -> v)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.