Skip to content

momentohq/client-sdk-dotnet

Repository files navigation

logo

project status project stability

Momento Client Library for .NET

Momento Cache is a fast, simple, pay-as-you-go caching solution without any of the operational overhead required by traditional caching solutions. This repo contains the source code for the Momento client library for .NET.

To get started with Momento you will need a Momento Auth Token. You can get one from the Momento Console.

Japanese: 日本語

Packages

The Momento Dotnet SDK package is available on nuget: momentohq/client-sdk-dotnet.

Prerequisites

  • dotnet 6.0 or higher is required
  • A Momento API key is required. You can generate one using the Momento Console.
  • A Momento service endpoint is required. Choose the one for the region you'll be using, e.g. cell-1-ap-southeast-1-1.prod.a.momentohq.com for ap-southeast-1

Usage

Here is a quickstart you can use in your own project:

using System;
using Momento.Sdk;
using Momento.Sdk.Auth;
using Momento.Sdk.Config;
using Momento.Sdk.Responses;

ICredentialProvider authProvider = new EnvMomentoV2TokenProvider();
const string CACHE_NAME = "cache";
const string KEY = "MyKey";
const string VALUE = "MyData";
TimeSpan DEFAULT_TTL = TimeSpan.FromSeconds(60);

using (ICacheClient client = new CacheClient(Configurations.Laptop.V1(), authProvider, DEFAULT_TTL))
{
    var createCacheResponse = await client.CreateCacheAsync(CACHE_NAME);
    if (createCacheResponse is CreateCacheResponse.Error createError)
    {
        Console.WriteLine($"Error creating cache: {createError.Message}. Exiting.");
        Environment.Exit(1);
    }

    Console.WriteLine($"Setting key: {KEY} with value: {VALUE}");
    var setResponse = await client.SetAsync(CACHE_NAME, KEY, VALUE);
    if (setResponse is CacheSetResponse.Error setError)
    {
        Console.WriteLine($"Error setting value: {setError.Message}. Exiting.");
        Environment.Exit(1);
    }

    Console.WriteLine($"Get value for key: {KEY}");
    CacheGetResponse getResponse = await client.GetAsync(CACHE_NAME, KEY);
    if (getResponse is CacheGetResponse.Hit hitResponse)
    {
        Console.WriteLine($"Looked up value: {hitResponse.ValueString}, Stored value: {VALUE}");
    }
    else if (getResponse is CacheGetResponse.Error getError)
    {
        Console.WriteLine($"Error getting value: {getError.Message}");
    }
}

Getting Started and Documentation

Documentation is available on the Momento Docs website.

Examples

Ready to dive right in? Just check out the examples directory for complete, working examples of how to use the SDK.

Developing

If you are interested in contributing to the SDK, please see the CONTRIBUTING docs.


For more info, visit our website at https://gomomento.com!

About

Official .NET SDK for Momento Serverless Cache

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 21