Skip to content

Commit

Permalink
Add a basic leaderboard example that uses a SNAPSHOT version of the sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
nand4011 committed Jan 10, 2025
1 parent ada265d commit 90233ce
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
11 changes: 10 additions & 1 deletion examples/cache/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ plugins {
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
maven {
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
}

dependencies {
implementation("software.momento.java:sdk:1.18.0")
implementation("software.momento.java:sdk:1.18.1-SNAPSHOT")

implementation("com.google.guava:guava:31.1-android")

Expand Down Expand Up @@ -117,3 +120,9 @@ task("docsTasks") {


task("prepareKotlinBuildScriptModel") {}

task("leaderboard", JavaExec::class) {
description = "Run the leaderboard example"
classpath = sourceSets.main.get().runtimeClasspath
mainClass.set("momento.client.example.LeaderboardExample")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package momento.client.example;

import momento.sdk.CacheClient;
import momento.sdk.ILeaderboard;
import momento.sdk.LeaderboardClient;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.auth.EnvVarCredentialProvider;
import momento.sdk.config.Configurations;
import momento.sdk.config.LeaderboardConfigurations;
import momento.sdk.exceptions.AlreadyExistsException;
import momento.sdk.responses.SortOrder;
import momento.sdk.responses.cache.GetResponse;
import momento.sdk.responses.cache.control.CacheCreateResponse;
import momento.sdk.responses.cache.control.CacheInfo;
import momento.sdk.responses.cache.control.CacheListResponse;
import momento.sdk.responses.leaderboard.FetchResponse;
import momento.sdk.responses.leaderboard.LeaderboardElement;
import momento.sdk.responses.leaderboard.UpsertResponse;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

public class LeaderboardExample {

private static final String API_KEY_ENV_VAR = "MOMENTO_API_KEY";
private static final Duration DEFAULT_ITEM_TTL = Duration.ofSeconds(60);

private static final String CACHE_NAME = "cache";
private static final String LEADERBOARD_NAME = "leaderboard";

public static void main(String[] args) {
final CredentialProvider credentialProvider = new EnvVarCredentialProvider(API_KEY_ENV_VAR);

try (final CacheClient cacheClient =
CacheClient.create(credentialProvider, Configurations.Laptop.latest(), DEFAULT_ITEM_TTL);
final LeaderboardClient leaderboardClient =
LeaderboardClient.builder(credentialProvider, LeaderboardConfigurations.Laptop.latest()).build()) {

cacheClient.createCache(CACHE_NAME).join();

final ILeaderboard leaderboard = leaderboardClient.leaderboard(CACHE_NAME, LEADERBOARD_NAME);

final Map<Integer, Double> elements = new HashMap<>();
elements.put(1, 1.0);
elements.put(2, 2.0);
elements.put(3, 3.0);

final UpsertResponse upsertResponse = leaderboard.upsert(elements).join();
if (upsertResponse instanceof UpsertResponse.Error error) {
System.out.println("Error during upsert: " + error.getMessage());
}

final FetchResponse fetchResponse = leaderboard.fetchByRank(0, 2, SortOrder.ASCENDING).join();
if (fetchResponse instanceof FetchResponse.Success success) {
for (LeaderboardElement element : success.elementsList()) {
System.out.printf("Rank: %d, ID: %d, Score: %.2f%n",
element.getRank(),
element.getId(),
element.getScore());
}
} else if (fetchResponse instanceof FetchResponse.Error error) {
System.out.println("Error during fetch: " + error.getMessage());
}
}
}
}

0 comments on commit 90233ce

Please sign in to comment.