Redis互換クライアントを使用してMomento Cache、Momento Topicsに切り替える方法
Redisキャッシュが使われている既存のアプリを、Momentoのサービスによるものに切り替えることを検討されていますか?Redis互換クライアントを使用すれば、コードのリファクタリングは必要ありません。既存のRedisクライアントは、ドロップインによって互換性のあるクライアントに置き換えることができます。コードの中核部分を変える必要はなく、クライアントライブラリを互換クライアントに変更し、接続情報を変更するだけでできます。
始めよう
クライアント・オブジェクトを構築するコードを変更するだけで、既存のアプリをMomento Cacheによるものに切り替えることができます:
- NodeRedis
- IORedis
- StackExchange
- Go
- redis-py
// 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(
new momento.CacheClient.create({
configuration: momento.Configurations.Laptop.v1(),
credentialProvider: momento.CredentialProvider.fromEnvironmentVariable({
environmentVariableName: 'MOMENTO_AUTH_TOKEN',
}),
defaultTtlSeconds: 60,
}),
'cache_name',
);
サンプルコードを含むより詳細な情報については、GithubのMomento Node.js Redis compatibility client をご覧ください。
// 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: authTokenEnvVarName,
}),
defaultTtlSeconds: config.defaultTTLSeconds,
}),
'cache_name',
);
サンプルコードを含むより詳細な情報については、GithubのMomento IORedis compatibility client をご覧ください。
using System;
using Momento.Auth;
using Momento.Config;
using Momento.Sdk;
using Momento.StackExchange.Redis;
// Create a Momento-backed Redis client
var db = MomentoRedisDatabase(
new CacheClient(
config: Configurations.Laptop.v1(),
authProvider: new EnvMomentoTokenProvider("MOMENTO_AUTH_TOKEN"),
defaultTtl: TimeSpan.FromSeconds(60),
}),
"cache_name"
);
サンプルコードを含むより詳細な情報については、GithubのMomento StackExchange compatibility client をご覧ください。
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_AUTH_TOKEN")
if eErr != nil {
panic("Failed to initialize credentials through auth token " + 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
}
サンプルコードを含むより詳細な情報については、Go-redis compatibility client をご覧ください。
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_AUTH_TOKEN"),
datetime.timedelta(seconds=60)
),
"cache_name"
)
サンプルコードを含むより詳細な情報については、Momento Python Redis compatibility client をご覧ください。
ソースコード
すべてのRedis互換クライアントのソースコードとサンプルについては、以下のGitHubリポジトリをご覧ください: