Skip to main content

Getting started with Momento Cache

If you're looking to jump in and get started with Momento Cache, you've come to the right place.

Pre-requisites

Follow our step-by-step instructions to create an API key via the Momento console. Come back here when you have your key.

Using the Momento console

Using the Momento CLI

You can also install the Momento CLI locally (Linux, Windows, Mac) if desired. The latest version of the CLI and instructions for installation and usage is available in this repo.

Using the Momento SDKs

SDK Installation and Example
There are multiple places you can store the API key you created. For this simple example, we'll store it in an environment variable, but the best practice is to store it in a secure location like AWS Secrets Manager.

Install the Momento SDK in your project directory

npm install @gomomento/sdk

Copy this code to a file test.js

const { CacheGet, CacheSet, Configurations, CacheClient, CredentialProvider } = require('@gomomento/sdk');

const CACHE_NAME = 'demo';

// A simple function that calls all functions in order. You probably want more error handling.
async function run() {
const cacheClient = await CacheClient.create({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: 'MOMENTO_API_KEY',
}),
defaultTtlSeconds: 600,
});
await client.createCache(CACHE_NAME);
// set cache key
const key = "key";
const data = 12345;
const setResponse = await client.set(cacheName, key, data);
if (setResponse instanceof CacheSet.Success) {
console.log('Key stored successfully!');
}
const readResponse = await client.get(cacheName, key);
if (readResponse instanceof CacheGet.Hit) {
console.log('Cache hit: ', readResponse.valueString());
} else if (readResponse instanceof CacheGet.Miss) {
console.log('Cache miss');
}
}

run();

Run code

MOMENTO_API_KEY='your_key_here' node test.js

The output should look something like this:

[2023-05-21T00:56:37.819Z] INFO (Momento: CacheClient): Creating Momento CacheClient
[2023-05-21T00:56:37.831Z] INFO (Momento: ControlClient): Creating cache: demo
Cache created.
Key stored successfully!
Cache hit: 12345