Skip to content

Commit

Permalink
feat: Add createOrGetCache method to the SDK (#86)
Browse files Browse the repository at this point in the history
* feat: Add createOrGetCache method to the SDK

* add test

* fix formatting
  • Loading branch information
gautamomento authored Oct 12, 2021
1 parent bf2fed5 commit 31a4ac6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
22 changes: 10 additions & 12 deletions momento-sdk/src/intTest/java/momento/sdk/MomentoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,20 @@ void testHappyPath() {
runHappyPathTest(momento, cacheName);
}

@Test
void recreatingCacheWithSameName_throwsAlreadyExists() {
Momento momento = Momento.builder(authToken).build();
momento.createOrGetCache(cacheName);
assertThrows(CacheAlreadyExistsException.class, () -> momento.createCache(cacheName));
}

@Test
void testInvalidCacheName() {
Momento momento =
Momento.builder(authToken).endpointOverride(DEFAULT_MOMENTO_HOSTED_ZONE_ENDPOINT).build();

assertThrows(InvalidArgumentException.class, () -> getOrCreate(momento, " "));
assertThrows(InvalidArgumentException.class, () -> momento.createCache(" "));
assertThrows(InvalidArgumentException.class, () -> momento.createOrGetCache(" "));
}

@Test
Expand All @@ -82,7 +90,7 @@ void deleteForNonExistantCache_throwsNotFound() {
}

private static void runHappyPathTest(Momento momento, String cacheName) {
Cache cache = getOrCreate(momento, cacheName);
Cache cache = momento.createOrGetCache(cacheName);

String key = java.util.UUID.randomUUID().toString();

Expand All @@ -96,14 +104,4 @@ private static void runHappyPathTest(Momento momento, String cacheName) {
assertEquals(MomentoCacheResult.Hit, rsp.result());
assertEquals("bar", rsp.asStringUtf8().get());
}

// TODO: Update this to be recreated each time and add a separate test case for Already Exists
private static Cache getOrCreate(Momento momento, String cacheName) {
try {
momento.createCache(cacheName);
} catch (CacheAlreadyExistsException e) {
// Just get Cache following a successful create
}
return momento.getCache(cacheName);
}
}
21 changes: 21 additions & 0 deletions momento-sdk/src/main/java/momento/sdk/Momento.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,27 @@ public DeleteCacheResponse deleteCache(String cacheName) {
}
}

/**
* Creates a client to interact with the given cache name.
*
* <p>If a cache with given name already exists then returns a client to interact with the cache.
* Otherwise, first creates a new Momento Cache and then returns a client to interact with the
* newly created cache.
*
* @param cacheName
* @return {@link Cache} client to perform operations against the Momento Cache with the provided
* name.
*/
public Cache createOrGetCache(String cacheName) {
checkCacheNameValid(cacheName);
try {
createCache(cacheName);
} catch (CacheAlreadyExistsException e) {
// This implies that cache with the given name already exists. Just create a client.
}
return getCache(cacheName);
}

public Cache getCache(String cacheName) {
checkCacheNameValid(cacheName);
return makeCacheClient(authToken, cacheName, momentoEndpoints.cacheEndpoint());
Expand Down

0 comments on commit 31a4ac6

Please sign in to comment.