Skip to content

Commit

Permalink
Added cache reset
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesDuboisSAP committed Sep 9, 2024
1 parent 163212e commit a481a9b
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions core/src/main/java/com/sap/ai/sdk/core/DeploymentCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import java.util.NoSuchElementException;
import javax.annotation.Nonnull;

/**
* Cache for deployment IDs. This class is used to get the deployment id for the orchestration
* scenario or for a model.
*/
class DeploymentCache {
private static final DeploymentApi API = new DeploymentApi(getClient());

Expand All @@ -18,18 +22,29 @@ class DeploymentCache {
*/
private static final Map<String, String> CACHE = new HashMap<>();

// Eagerly load all deployments into the cache.
static {
resetCache();
}

/**
* Remove all entries from the cache and reload all deployments.
*
* <p><b>Call this method if you delete a deployment.</b>
*/
public static void resetCache() {
CACHE.clear();
final var deployments = API.deploymentQuery("default").getResources();
deployments.forEach(
deployment ->
CACHE.put(deployment.getScenarioId() + getModelName(deployment), deployment.getId()));
}

/**
* Get the deployment id for the given scenario or model name.
* Get the deployment id for the orchestration scenario or any foundation model.
*
* @param resourceGroup the resource group, usually "default".
* @param name the scenario or model name.
* @param name "orchestration" or the model name.
* @return the deployment id.
*/
public static String getDeploymentId(
Expand Down Expand Up @@ -73,8 +88,8 @@ private static String getOrchestrationDeployment(@Nonnull final String resourceG
* Get the deployment id from the model name. If there are multiple deployments of the same model,
* the first one is returned.
*
* @param modelName the model name.
* @param resourceGroup the resource group.
* @param modelName the model name.
* @return the deployment id
* @throws NoSuchElementException if no deployment is found for the model name.
*/
Expand Down

0 comments on commit a481a9b

Please sign in to comment.