Skip to main content

Getting Started with Momento Cache in .NET

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

Install the Momento SDK

Install the client library in an existing .NET project:

dotnet add package Momento.Sdk

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 API key, store it in an environment variable so that the Momento client can consume it:

export MOMENTO_API_KEY=<your Momento 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.

Import libraries and create a CacheClient object

This example file pulls in the necessary imports, reads the API key from an environment variable, and instantiates the CacheClient that is used to interact with a cache.

Create a new cache in Momento Cache

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

var result = await cacheClient.CreateCacheAsync("test-cache");
if (result is CreateCacheResponse.Success)
Console.WriteLine("Cache 'test-cache' created");
else if (result is CreateCacheResponse.CacheAlreadyExists)
Console.WriteLine("Cache 'test-cache' already exists");
else if (result is CreateCacheResponse.Error error)
throw new Exception($"An error occurred while attempting to create cache 'test-cache': {error.ErrorCode}: {error}");

List the names of existing caches in your account

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

var result = await cacheClient.ListCachesAsync();
if (result is ListCachesResponse.Success success)
Console.WriteLine($"Caches:\n{string.Join("\n", success.Caches.Select(c => c.Name))}\n\n");
else if (result is ListCachesResponse.Error error)
throw new Exception($"An error occurred while attempting to list caches: {error.ErrorCode}: {error}");

Write an item to a cache

A simple example of doing a set operation. In the client.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.

var result = await cacheClient.SetAsync("test-cache", "test-key", "test-value");
if (result is CacheSetResponse.Success)
Console.WriteLine("Key 'test-key' stored successfully");
else if (result is CacheSetResponse.Error error)
throw new Exception($"An error occurred while attempting to store key 'test-key' in cache 'test-cache': {error.ErrorCode}: {error}");

Read an item from a cache

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

var result = await cacheClient.GetAsync("test-cache", "test-key");
if (result is CacheGetResponse.Hit hit)
Console.WriteLine($"Retrieved value for key 'test-key': {hit.ValueString}");
else if (result is CacheGetResponse.Miss)
Console.WriteLine("Key 'test-key' was not found in cache 'test-cache'");
else if (result is CacheGetResponse.Error error)
throw new Exception($"An error occurred while attempting to get key 'test-key' from cache 'test-cache': {error.ErrorCode}: {error}");

Running the code

You can find complete, working examples in the csharp SDK GitHub repo examples directory.


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

Check out this example code for more advanced calls.