Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Cluster } from 'ioredis';
import { Cluster, ClusterOptions } from 'ioredis';
import { ClientOptions } from '../../types/aws';
export interface Singleton {
[key: string]: Cluster;
Expand All @@ -7,6 +7,7 @@ export declare const singleton: Singleton;
export interface ElastiCacheRedisClientOptions extends ClientOptions {
endpoint?: string;
port?: number;
clusterOptions?: ClusterOptions;
}
declare const getElastiCacheRedisClient: (clientOpts?: ElastiCacheRedisClientOptions) => Promise<Cluster>;
export default getElastiCacheRedisClient;
55 changes: 36 additions & 19 deletions dist/services/ElastiCacheRedisService/getElastiCacheRedisClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,30 @@ import { elasticache as elasticacheConfig } from '../../config';
import { logger, isEmpty, validateFields } from '../../utils';
import { LesgoException } from '../../exceptions';
const FILE = 'lesgo.services.ElastiCacheRedisService.getClient';
const REDIS_CONN_TIMEOUT = 5000;
export const singleton = {};
const waitForClusterReady = redisClient => {
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
reject(new Error('ElastiCache Redis connection timed out'));
}, REDIS_CONN_TIMEOUT);
redisClient.once('ready', () => {
clearTimeout(timeout);
resolve();
});
redisClient.once('error', err => {
clearTimeout(timeout);
reject(`ElastiCache Redis: ${err.message}`);
});
});
};
const getElastiCacheRedisClient = clientOpts =>
__awaiter(void 0, void 0, void 0, function* () {
const options = validateFields(clientOpts || {}, [
{ key: 'singletonConn', type: 'string', required: false },
{ key: 'endpoint', type: 'string', required: false },
{ key: 'port', type: 'number', required: false },
{ key: 'clusterOptions', type: 'object', required: false },
]);
const singletonConn = options.singletonConn || 'default';
if (!isEmpty(singleton[singletonConn])) {
Expand All @@ -52,6 +69,10 @@ const getElastiCacheRedisClient = clientOpts =>
const clusterEndpoint =
options.endpoint || elasticacheConfig.redis.endpoint;
const clusterPort = options.port || elasticacheConfig.redis.port || 6379;
const clusterOptions = Object.assign(
{ dnsLookup: (address, callback) => callback(null, address) },
options.clusterOptions || {}
);
if (!clusterEndpoint) {
throw new LesgoException(
'Missing ElastiCache Redis endpoint',
Expand All @@ -67,40 +88,36 @@ const getElastiCacheRedisClient = clientOpts =>
port: clusterPort,
},
],
{
dnsLookup: (address, callback) => callback(null, address),
redisOptions: {
tls: {},
},
}
clusterOptions
);
redisClient.on('error', err => {
logger.error(`${FILE}::REDIS_CLIENT_NOT_CONNECTED_ERROR`, { err });
});
redisClient.on('connect', () => {
logger.debug(`${FILE}::REDIS_CLIENT_CONNECTED`);
});
try {
yield redisClient.connect();
yield waitForClusterReady(redisClient);
} catch (error) {
// Not ideal to base on error message but ioredis doesn't have a better way to check if it's already connected
if (error.message === 'Redis is already connecting/connected') {
const errorMessage =
typeof error === 'string'
? error
: error === null || error === void 0
? void 0
: error.message;
if (
errorMessage ===
'ElastiCache Redis: Redis is already connecting/connected'
) {
logger.debug(`${FILE}::REDIS_ALREADY_CONNECTED`, {
error,
errorMessage:
error === null || error === void 0 ? void 0 : error.message,
errorMessage,
clusterEndpoint,
clusterPort,
});
} else {
throw new LesgoException(
'Failed to connect to ElastiCache Redis',
errorMessage,
`${FILE}::REDIS_CONNECT_ERROR`,
500,
{
error,
errorMessage:
error === null || error === void 0 ? void 0 : error.message,
errorMessage,
clusterEndpoint,
clusterPort,
}
Expand Down
3 changes: 2 additions & 1 deletion dist/services/ElastiCacheRedisService/setRedisCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const setRedisCache = (key, value, opts, clientOpts) =>
var _a;
const input = validateFields({ key, value }, [
{ key: 'key', type: 'string', required: true },
{ key: 'value', type: 'any', required: true },
// Can accept empty values to caching
{ key: 'value', type: 'any', required: false },
]);
opts = Object.assign(Object.assign({}, opts), {
EX:
Expand Down
6 changes: 6 additions & 0 deletions dist/types/aws.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ClusterOptions } from 'ioredis';
export interface ClientOptions {
region?: string;
singletonConn?: string;
}
export interface ElastiCacheRedisClientOptions extends ClientOptions {
region?: string;
singletonConn?: string;
clusterOptions?: ClusterOptions;
}
export interface RDSAuroraMySQLProxyClientOptions extends ClientOptions {
dbCredentialsSecretId?: string;
databaseName?: string;
Expand Down
6 changes: 3 additions & 3 deletions dist/utils/cache/redis/deleteCache.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ClientOptions } from '../../../types/aws';
import { ElastiCacheRedisClientOptions } from '../../../types/aws';
/**
* Deletes the cache value from the Redis cache.
*
* @param {string | string[]} keys - The key(s) of the cache value to delete.
* @param {ClientOptions} clientOpts - Optional client options for Redis connection.
* @param {ElastiCacheRedisClientOptions} clientOpts - Optional client options for Redis connection.
* @returns A promise that resolves to the deleted cache value.
* @throws {LesgoException} If there is an error deleting the cache.
*
Expand All @@ -16,5 +16,5 @@ import { ClientOptions } from '../../../types/aws';
*
* await deleteCache(keys);
*/
declare const deleteCache: (keys: string | string[], clientOpts?: ClientOptions) => Promise<void>;
declare const deleteCache: (keys: string | string[], clientOpts?: ElastiCacheRedisClientOptions) => Promise<void>;
export default deleteCache;
2 changes: 1 addition & 1 deletion dist/utils/cache/redis/deleteCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { deleteRedisCache } from '../../../services/ElastiCacheRedisService';
* Deletes the cache value from the Redis cache.
*
* @param {string | string[]} keys - The key(s) of the cache value to delete.
* @param {ClientOptions} clientOpts - Optional client options for Redis connection.
* @param {ElastiCacheRedisClientOptions} clientOpts - Optional client options for Redis connection.
* @returns A promise that resolves to the deleted cache value.
* @throws {LesgoException} If there is an error deleting the cache.
*
Expand Down
6 changes: 3 additions & 3 deletions dist/utils/cache/redis/getCache.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ClientOptions } from '../../../types/aws';
import { ElastiCacheRedisClientOptions } from '../../../types/aws';
/**
* Retrieves the value from the cache based on the provided key.
*
* @param {string} key - The key used to identify the value in the cache.
* @param {ClientOptions} clientOpts - Optional client options for the cache client.
* @param {ElastiCacheRedisClientOptions} clientOpts - Optional client options for the cache client.
* @returns A promise that resolves when the value is retrieved from the cache.
*
* @throws {LesgoException} If there is an error retrieving the cache.
Expand All @@ -18,5 +18,5 @@ import { ClientOptions } from '../../../types/aws';
* console.log(value); // Value retrieved from the cache
* ```
*/
declare const getCache: (key: string, clientOpts?: ClientOptions) => Promise<any>;
declare const getCache: (key: string, clientOpts?: ElastiCacheRedisClientOptions) => Promise<any>;
export default getCache;
2 changes: 1 addition & 1 deletion dist/utils/cache/redis/getCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getRedisCache } from '../../../services/ElastiCacheRedisService';
* Retrieves the value from the cache based on the provided key.
*
* @param {string} key - The key used to identify the value in the cache.
* @param {ClientOptions} clientOpts - Optional client options for the cache client.
* @param {ElastiCacheRedisClientOptions} clientOpts - Optional client options for the cache client.
* @returns A promise that resolves when the value is retrieved from the cache.
*
* @throws {LesgoException} If there is an error retrieving the cache.
Expand Down
6 changes: 3 additions & 3 deletions dist/utils/cache/redis/getClient.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientOptions } from '../../../types/aws';
import { ElastiCacheRedisClientOptions } from '../../../types/aws';
/**
* Retrieves a Redis client.
*
Expand All @@ -9,7 +9,7 @@ import { ClientOptions } from '../../../types/aws';
* This function is not intended to be used directly.
* Use the available `getCache`, `setCache`, and `deleteCache` functions instead.
*
* @param {ClientOptions} clientOpts - Optional client options.
* @param {ElastiCacheRedisClientOptions} clientOpts - Optional client options.
* @returns A promise of with Redis client.
*
* @throws {LesgoException} If there is an error creating the client.
Expand All @@ -26,5 +26,5 @@ import { ClientOptions } from '../../../types/aws';
* await client.set(key, value);
* ```
*/
declare const getClient: (clientOpts?: ClientOptions) => Promise<import("ioredis/built/cluster").default>;
declare const getClient: (clientOpts?: ElastiCacheRedisClientOptions) => Promise<import("ioredis/built/cluster").default>;
export default getClient;
2 changes: 1 addition & 1 deletion dist/utils/cache/redis/getClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getElastiCacheRedisClient } from '../../../services/ElastiCacheRedisSer
* This function is not intended to be used directly.
* Use the available `getCache`, `setCache`, and `deleteCache` functions instead.
*
* @param {ClientOptions} clientOpts - Optional client options.
* @param {ElastiCacheRedisClientOptions} clientOpts - Optional client options.
* @returns A promise of with Redis client.
*
* @throws {LesgoException} If there is an error creating the client.
Expand Down
6 changes: 3 additions & 3 deletions dist/utils/cache/redis/scanCache.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ClientOptions } from '../../../types/aws';
import { ElastiCacheRedisClientOptions } from '../../../types/aws';
/**
* Scans the Redis cache for keys matching a given pattern.
*
* @param {string} pattern - The pattern to match keys against.
* @param {ClientOptions} clientOpts - Optional client options for the cache client.
* @param {ElastiCacheRedisClientOptions} clientOpts - Optional client options for the cache client.
* @returns A promise that resolves when the value is retrieved from the cache.
*
* @throws {LesgoException} If there is an error retrieving the cache.
Expand All @@ -19,5 +19,5 @@ import { ClientOptions } from '../../../types/aws';
* console.log(keys); // Array of cache keys returned from the scan operation
* ```
*/
declare const scanCache: (pattern: string, clientOpts?: ClientOptions) => Promise<string[]>;
declare const scanCache: (pattern: string, clientOpts?: ElastiCacheRedisClientOptions) => Promise<string[]>;
export default scanCache;
2 changes: 1 addition & 1 deletion dist/utils/cache/redis/scanCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { scanRedisCache } from '../../../services/ElastiCacheRedisService';
* Scans the Redis cache for keys matching a given pattern.
*
* @param {string} pattern - The pattern to match keys against.
* @param {ClientOptions} clientOpts - Optional client options for the cache client.
* @param {ElastiCacheRedisClientOptions} clientOpts - Optional client options for the cache client.
* @returns A promise that resolves when the value is retrieved from the cache.
*
* @throws {LesgoException} If there is an error retrieving the cache.
Expand Down
6 changes: 3 additions & 3 deletions dist/utils/cache/redis/setCache.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ClientOptions } from '../../../types/aws';
import { ElastiCacheRedisClientOptions } from '../../../types/aws';
import { SetRedisCacheOptions } from '../../../services/ElastiCacheRedisService/setRedisCache';
/**
* Sets a value in the Redis cache.
*
* @param {string} key - The key to set in the cache.
* @param {any} value - The value to set in the cache.
* @param {SetRedisCacheOptions} opts - Optional settings for setting the cache.
* @param {ClientOptions} clientOpts - Optional client options for connecting to Redis.
* @param {ElastiCacheRedisClientOptions} clientOpts - Optional client options for connecting to Redis.
* @returns A promise that resolves when the value is successfully set in the cache.
*
* @throws {LesgoException} If there is an error setting the cache.
Expand All @@ -21,5 +21,5 @@ import { SetRedisCacheOptions } from '../../../services/ElastiCacheRedisService/
* await setCache(key, value);
* ```
*/
declare const setCache: (key: string, value: any, opts?: SetRedisCacheOptions, clientOpts?: ClientOptions) => Promise<"OK">;
declare const setCache: (key: string, value: any, opts?: SetRedisCacheOptions, clientOpts?: ElastiCacheRedisClientOptions) => Promise<"OK">;
export default setCache;
2 changes: 1 addition & 1 deletion dist/utils/cache/redis/setCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { setRedisCache } from '../../../services/ElastiCacheRedisService';
* @param {string} key - The key to set in the cache.
* @param {any} value - The value to set in the cache.
* @param {SetRedisCacheOptions} opts - Optional settings for setting the cache.
* @param {ClientOptions} clientOpts - Optional client options for connecting to Redis.
* @param {ElastiCacheRedisClientOptions} clientOpts - Optional client options for connecting to Redis.
* @returns A promise that resolves when the value is successfully set in the cache.
*
* @throws {LesgoException} If there is an error setting the cache.
Expand Down
Loading