Redis compatibility clients for Momento Cache and Momento Topics
Do you have existing apps that use a Redis cache, but now you're trading up to Momento’s services? With Redis compatibility clients, there's no need to refactor your code. Instead, compatibility clients are a drop-in replacement for existing Redis clients. Change your client library to the compatibility client, change the connection information, and the core of your code stays the same.
Getting Started
To switch your existing application to use Momento Cache, you only need to change the code where you construct your client object:
- NodeRedis
- IORedis
- StackExchange
- Go
- redis-py
- lettuce
- PhpRedis
// Import the Momento redis compatibility client.
import {createClient, momento} from 'momento-redis-client';
import {
CacheClient,
Configurations,
CredentialProvider,
} from '@gomomento/sdk';
// Initialize Momento's client.
const redisClient = createClient(
momento.CacheClient.create({
configuration: momento.Configurations.Laptop.v1(),
credentialProvider: momento.CredentialProvider.fromEnvironmentVariable({
environmentVariableName: 'MOMENTO_API_KEY',
}),
defaultTtlSeconds: 60,
}),
'cache_name',
);
For more in-depth information, with example code, please see Momento Node.js Redis compatibility client on GitHub.
// Import the Momento redis compatibility client.
import {MomentoRedisAdapter} from '@gomomento-poc/node-ioredis-client';
import {
CacheClient,
Configurations,
CredentialProvider,
} from '@gomomento/sdk';
// Instantiate Momento Adapter Directly
const Redis = new MomentoRedisAdapter(
await CacheClient.create({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: 'MOMENTO_API_KEY',
}),
defaultTtlSeconds: config.defaultTTLSeconds,
}),
'cache_name',
);
For more in-depth information, with example code, please see Momento IORedis compatibility client on GitHub.
using System;
using Momento.Auth;
using Momento.Config;
using Momento.Sdk;
using Momento.StackExchange.Redis;
// Create a Momento-backed Redis client
var db = MomentoRedisDatabase(
await CacheClient.create(
config: Configurations.Laptop.v1(),
authProvider: new EnvMomentoTokenProvider("MOMENTO_API_KEY"),
defaultTtl: TimeSpan.FromSeconds(60),
}),
"cache_name"
);
For more in-depth information, with example code, please see Momento StackExchange compatibility client on GitHub.
package redis
import (
"context"
"github.com/momentohq/client-sdk-go/auth"
"github.com/momentohq/client-sdk-go/config"
"github.com/momentohq/client-sdk-go/momento"
momentoredis "github.com/momentohq/momento-go-redis-client/momento-redis"
"github.com/redis/go-redis/v9"
"time"
)
func initRedisClient() redis.Cmdable {
credential, eErr := auth.NewEnvMomentoTokenProvider("MOMENTO_API_KEY")
if eErr != nil {
panic("Failed to initialize credentials through API key " + eErr.Error())
}
cacheClient, cErr := momento.NewCacheClient(config.LaptopLatest(), credential, 60*time.Second)
if cErr != nil {
panic("Failed to initialize Momento cache client " + cErr.Error())
}
// create cache; it resumes execution normally incase the cache already exists
_, createErr := cacheClient.CreateCache(context.Background(),
&momento.CreateCacheRequest{CacheName: "default_cache"})
if createErr != nil {
panic("Failed to create cache with cache name default cache \n" + createErr.Error())
}
redisClient := momentoredis.NewMomentoRedisClient(cacheClient, "default_cache")
return redisClient
}
For more in-depth information, with example code, please see Go-redis compatibility client on GitHub.
import datetime
# Import the Momento redis compatibility client.
import momento
from momento_redis import MomentoRedis
# Initialize Momento client.
redis_client = MomentoRedis(
momento.CacheClient.create(
momento.Configurations.Laptop.latest(),
momento.CredentialProvider.from_environment_variable("MOMENTO_API_KEY"),
datetime.timedelta(seconds=60)
),
"cache_name"
)
For more in-depth information, with example code, please see Momento Python Redis compatibility client on GitHub.
import io.lettuce.core.api.reactive.RedisReactiveCommands;
import java.time.Duration;
import momento.lettuce.MomentoRedisReactiveClient;
import momento.sdk.CacheClient;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.config.Configurations;
class CompatibilityExample {
public static void main(String[] args) {
// Use try-with-resources to ensure proper closing of CacheClient
try (CacheClient cacheClient = CacheClient.create(
CredentialProvider.fromEnvironmentVariable("MOMENTO_API_KEY"),
Configurations.Laptop.v1(),
Duration.ofSeconds(60)
)) {
// Create a Redis client backed by the Momento cache client
RedisReactiveCommands<String, String> redisClient =
MomentoRedisReactiveClient.create(cacheClient, "cache_name");
}
}
}
<?php
declare(strict_types=1);
use Momento\Auth\CredentialProvider;
use Momento\Cache\CacheClient;
use Momento\Cache\MomentoCacheClient;
use Momento\Config\Configurations\Laptop;
use Momento\Logging\StderrLoggerFactory;
require "vendor/autoload.php";
// Create a Momento cache client
$authProvider = CredentialProvider::fromEnvironmentVariable("MOMENTO_API_KEY");
$configuration = Laptop::latest(new StderrLoggerFactory());
$client = new CacheClient($configuration, $authProvider, 60);
// Create a Redis client backed by Momento cache client over the cache
$momentoCacheClient = new MomentoCacheClient($client, "cache_name");
Source Code
For source code and examples for all of our Redis compatibility clients, please see the following GitHub repositories: