Skip to content

Commit

Permalink
feat: make the environment variable names for the wrappers more consi…
Browse files Browse the repository at this point in the history
…stent (#26)
  • Loading branch information
cprice404 authored Apr 13, 2024
1 parent a9ac91e commit 612bf26
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
12 changes: 6 additions & 6 deletions README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ const redisClusterClient = NewIORedisClusterWrapper([], {});

In this package we provide wrapper functions that help you configure wether or not to use Momento and how client settings should look based off environment variables. This is to try and make for a simpler drop in experience where you might be running Momento or Redis based off the environment or Region. This applies for `NewIORedisWrapper` and `NewIORedisClusterWrapper` wrapper functions.

| EnvVar Name | Description | Default |
|---------------------|------------------------------------------------------------|---------|
| MOMENTO_ENABLED | Will allow you to toggle between using Momento and IORedis | false |
| MOMENTO_API_KEY | The Momento Auth token you would like to use | "" |
| CACHE_NAME | The name of the Momento Cache to use if Momento is enabled | "" |
| DEFAULT_TTL_SECONDS | The number of seconds to cache items for by default | 86400 |
| EnvVar Name | Description | Default |
|-----------------------------|------------------------------------------------------------|---------|
| MOMENTO_ENABLED | Will allow you to toggle between using Momento and IORedis | false |
| MOMENTO_API_KEY | The Momento Auth token you would like to use | "" |
| MOMENTO_CACHE_NAME | The name of the Momento Cache to use if Momento is enabled | "" |
| MOMENTO_DEFAULT_TTL_SECONDS | The number of seconds to cache items for by default | 86400 |

## Installation

Expand Down
31 changes: 19 additions & 12 deletions src/wrap-ioredis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,32 @@ interface config {
cacheName: string;
}

const authTokenEnvVarName = 'MOMENTO_API_KEY';
const momentoApiKeyEnvVarName = 'MOMENTO_API_KEY';
const momentoEnabledEnvVarName = 'MOMENTO_ENABLED';
const momentoCacheNameEnvVarName = 'MOMENTO_CACHE_NAME';
const momentoDefaultTtlEnvVarName = 'MOMENTO_DEFAULT_TTL_SECONDS';

function parseConfig(): config {
const enableMomentoVar = process.env['MOMENTO_ENABLED'],
defaultTTLSecondsVar = process.env['DEFAULT_TTL_SECONDS'],
cacheNameVar = process.env['CACHE_NAME'];
const enableMomentoVar = process.env[momentoEnabledEnvVarName],
defaultTTLSecondsVar = process.env[momentoDefaultTtlEnvVarName],
cacheNameVar = process.env[momentoCacheNameEnvVarName];
let enableMomento = false,
defaultTTLSeconds = 86400,
cacheName = '';

if (enableMomentoVar !== undefined && enableMomentoVar === 'true') {
enableMomento = true;
if (defaultTTLSecondsVar === undefined) {
throw new Error('missing DEFAULT_TTL env var when using momento');
throw new Error(
`missing ${momentoDefaultTtlEnvVarName} env var when using momento`
);
} else {
defaultTTLSeconds = Number.parseInt(defaultTTLSecondsVar);
}
if (cacheNameVar === undefined || cacheNameVar === '') {
throw new Error('missing CACHE_NAME env var when using momento');
throw new Error(
`missing ${momentoCacheNameEnvVarName} env var when using momento`
);
} else {
cacheName = cacheNameVar;
}
Expand All @@ -49,9 +56,9 @@ export function NewIORedisWrapper(options?: RedisOptions): MomentoIORedis {
return new MomentoRedisAdapter(
new CacheClient({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: authTokenEnvVarName,
}),
credentialProvider: CredentialProvider.fromEnvironmentVariable(
momentoApiKeyEnvVarName
),
defaultTtlSeconds: config.defaultTTLSeconds,
}),
config.cacheName
Expand All @@ -74,9 +81,9 @@ export function NewIORedisClusterWrapper(
return new MomentoRedisAdapter(
new CacheClient({
configuration: Configurations.Laptop.v1(),
credentialProvider: CredentialProvider.fromEnvironmentVariable({
environmentVariableName: authTokenEnvVarName,
}),
credentialProvider: CredentialProvider.fromEnvironmentVariable(
momentoApiKeyEnvVarName
),
defaultTtlSeconds: config.defaultTTLSeconds,
}),
config.cacheName
Expand Down

0 comments on commit 612bf26

Please sign in to comment.