Skip to main content

Getting Started with Momento Cache in Kotlin

If you need to get going quickly with Kotlin and Momento Cache, this page contains the basic API calls you'll need. Check the Kotlin SDK examples for complete, working examples including build configuration files.

Install the Momento SDK

The Momento SDK is available on Maven Central: `software.momento.kotlin/sdk.

tip

Visit Maven Central to find the latest available version of the SDK.

Install the client library in an existing Kotlin project:

Gradle

implementation("software.momento.kotlin:sdk:x.x.x")

Maven

<dependency>
<groupId>software.momento.kotlin</groupId>
<artifactId>sdk</artifactId>
<version>x.x.x</version>
</dependency>

Set up your API key

You'll need a Momento API key to authenticate with Momento. You can get one from the Momento Web Console. Once you have your token, store it in an environment variable so that the Momento client can consume it:

export MOMENTO_API_KEY=<your Momento API key here>

Import libraries and connect to return a CacheClient object

This code sets up the main function, the necessary imports, and the CacheClient instantiation that each of the other functions will need to call.

package software.momento.example.doc_examples

import kotlinx.coroutines.runBlocking
import software.momento.kotlin.sdk.CacheClient
import software.momento.kotlin.sdk.auth.CredentialProvider
import software.momento.kotlin.sdk.config.Configurations
import software.momento.kotlin.sdk.responses.cache.GetResponse
import kotlin.time.Duration.Companion.seconds

fun main() = runBlocking {
CacheClient(
CredentialProvider.fromEnvVar("MOMENTO_API_KEY"),
Configurations.Laptop.latest,
60.seconds
).use { client ->
//...
}
}

Create a new cache in Momento Cache

Use this function to create a new cache in your account.

when (val response = cacheClient.createCache("test-cache")) {
is CacheCreateResponse.Success -> println("Cache 'test-cache' created")
is CacheCreateResponse.AlreadyExists -> println("Cache 'test-cache' already exists")
is CacheCreateResponse.Error -> throw RuntimeException(
"An error occurred while attempting to create cache 'test-cache': ${response.errorCode}", response
)
}

List the names of existing caches in your account

A simple list of the names of caches for the account.

when (val response: CacheListResponse = cacheClient.listCaches()) {
is CacheListResponse.Success -> {
val caches: String = response.caches.joinToString("\n") { cacheInfo -> cacheInfo.name }
println("Caches:\n$caches")
}

is CacheListResponse.Error -> throw RuntimeException(
"An error occurred while attempting to list caches: ${response.errorCode}", response
)
}

Write an item to a cache

A simple example of doing a set operation. In the client.set call, the TTL it optional. If you did pass it in, this would override the default TTL value set with the client connection object.

when (val response = cacheClient.set("test-cache", "test-key", "test-value")) {
is SetResponse.Success -> println("Key 'test-key' stored successfully")
is SetResponse.Error -> throw RuntimeException(
"An error occurred while attempting to store key 'test-key' in cache 'test-cache': ${response.errorCode}",
response
)
}

Read an item from a cache

This is an example of a simple read operation to get an item from a cache.

when (val response = cacheClient.get("test-cache", "test-key")) {
is GetResponse.Hit -> println("Retrieved value for key 'test-key': ${response.value}")
is GetResponse.Miss -> println("Key 'test-key' was not found in cache 'test-cache'")
is GetResponse.Error -> throw RuntimeException(
"An error occurred while attempting to get key 'test-key' from cache 'test-cache': ${response.errorCode}",
response
)
}

Running the code

You can find complete, working examples in the Kotlin SDK github repo examples directory.

info

Beyond these basic API calls check out the API reference page for more information on the full assortment of Momento API calls.