Cloudflare Docs
Workers
Edit this page on GitHub
Set theme to dark (⇧+D)

Momento

Momento is a truly serverless caching service. It automatically optimizes, scales, and manages your cache for you.

This integration allows you to connect to Momento from your Worker by getting Momento cache configuration and adding it as secrets to your Worker.

​​ Momento Cache

To set up an integration with Momento Cache:

  1. You need to have an existing Momento cache to connect to or create a new cache through the Momento console.

  2. If you do not have an existing cache, create one and assign user-profiles as the cache name.

  3. Add the Momento database integration to your Worker:

    1. Log in to the Cloudflare dashboard and select your account.
    2. In Account Home, select Workers & Pages.
    3. In Overview, select your Worker.
    4. Select Integrations > Momento.
    5. Follow the setup flow, review and grant permissions needed to add secrets to your Worker.
    6. Next, connect to Momento.
    7. Select a preferred region.
    8. Click Add integration.
  4. The following example code show how to set an item in your cache, get it, and return it as a JSON object. The credentials needed to connect to Momento Cache have been automatically added as secrets to your Worker through the integration.

    export default {
    async fetch(request, env, ctx): Promise<Response> {
    const client = new MomentoFetcher(env.MOMENTO_API_KEY, env.MOMENTO_REST_ENDPOINT);
    const cache = env.MOMENTO_CACHE_NAME;
    const key = 'user';
    const f_name = 'mo';
    const l_name = 'squirrel';
    const value = `${f_name}_${l_name}`;
    // set a value into cache
    const setResponse = await client.set(cache, key, value);
    console.log('setResponse', setResponse);
    // read a value from cache
    const getResponse = await client.get(cache, key);
    console.log('getResponse', getResponse);
    return new Response(JSON.stringify({
    response: getResponse
    }));
    },
    } satisfies ExportedHandler<Env>;

To learn more about Momento, refer to Momento’s official documentation.