Skip to main content

Getting Started with Momento Cache in Dart

If you need to get going quickly with Dart and Momento Cache, this page contains the basic API calls you'll need. Check the Dart SDK examples for complete, working code samples.

Install the Momento SDK

The Momento Dart SDK is available on pub.dev as momento.

To install in your Dart program, use:

dart pub add momento

To install in your Flutter program, use:

flutter pub add momento

Get your Momento API key

You'll need a Momento API key. 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 api key here>

Note: it is best practice to put the API key into something like AWS Secret Manager or GCP Secret Manager instead of an environment variable for enhanced security, but we are using one here for demo purposes.

Set up a CacheClient

This code creates the CacheClient that you will use to call the other methods.

try {
final cacheClient = CacheClient(
CredentialProvider.fromEnvironmentVariable("MOMENTO_API_KEY"),
CacheClientConfigurations.latest(),
Duration(seconds: 30));
} catch (e) {
print("Unable to create cache client: $e");
exit(1);
}

Create a new cache in Momento Cache

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

final result = await cacheClient.createCache("test-cache");
switch (result) {
case CreateCacheAlreadyExists():
print("Cache already exists");
case CreateCacheError():
print("Error creating cache: $result");
case CreateCacheSuccess():
print("Successfully created the cache");
}

List the names of existing caches in your account

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

final result = await cacheClient.listCaches();
switch (result) {
case ListCachesError():
print("Error listing caches: $result");
case ListCachesSuccess():
print("Successfully listed caches: ${result.cacheNames}");
}

Delete a cache

Use this function to delete a cache from your account.

final result = await cacheClient.deleteCache("test-cache");
switch (result) {
case DeleteCacheError():
print("Error deleting cache: $result");
exit(1);
case DeleteCacheSuccess():
print("Successfully deleted cache");
}

Write an item to a cache

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

final result = await cacheClient.set("test-cache", "test-key", "test-value");
switch (result) {
case SetError():
print("Error setting key: $result");
exit(1);
case SetSuccess():
print("Successfully set item in the cache");
}

Read an item from a cache

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

final result = await cacheClient.get("test-cache", "test-key");
switch (result) {
case GetMiss():
print("Key was not found in cache.");
case GetError():
print("Error getting key: $result");
case GetHit():
print("Successfully got item from cache: ${result.value}");
}

Delete an item from a cache

This is an example of deleting an item from a cache.

final result = await cacheClient.delete("test-cache", "test-key");
switch (result) {
case DeleteError():
print("Error deleting key: $result");
exit(1);
case DeleteSuccess():
print("Successfully deleted key from cache");
}

Running the code

You can find complete, working examples in the Dart 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.