feature/IO-3556-Chattr-Integration - Add in Redis caching for Chatter
This commit is contained in:
@@ -8,6 +8,12 @@ const client = require("../graphql-client/graphql-client").client;
|
||||
*/
|
||||
const BODYSHOP_CACHE_TTL = 3600; // 1 hour
|
||||
|
||||
/**
|
||||
* Chatter API token cache TTL in seconds
|
||||
* @type {number}
|
||||
*/
|
||||
const CHATTER_TOKEN_CACHE_TTL = 3600; // 1 hour
|
||||
|
||||
/**
|
||||
* Generate a cache key for a bodyshop
|
||||
* @param bodyshopId
|
||||
@@ -15,6 +21,13 @@ const BODYSHOP_CACHE_TTL = 3600; // 1 hour
|
||||
*/
|
||||
const getBodyshopCacheKey = (bodyshopId) => `bodyshop-cache:${bodyshopId}`;
|
||||
|
||||
/**
|
||||
* Generate a cache key for a Chatter API token
|
||||
* @param companyId
|
||||
* @returns {`chatter-token:${string}`}
|
||||
*/
|
||||
const getChatterTokenCacheKey = (companyId) => `chatter-token:${companyId}`;
|
||||
|
||||
/**
|
||||
* Generate a cache key for a user socket mapping
|
||||
* @param email
|
||||
@@ -373,9 +386,53 @@ const applyRedisHelpers = ({ pubClient, app, logger }) => {
|
||||
*/
|
||||
const getProviderCache = (ns, field) => getSessionData(`${ns}:provider`, field);
|
||||
|
||||
/**
|
||||
* Get Chatter API token from Redis cache
|
||||
* @param companyId
|
||||
* @returns {Promise<string|null>}
|
||||
*/
|
||||
const getChatterToken = async (companyId) => {
|
||||
const key = getChatterTokenCacheKey(companyId);
|
||||
try {
|
||||
const token = await pubClient.get(key);
|
||||
return token;
|
||||
} catch (error) {
|
||||
logger.log("get-chatter-token-from-redis", "ERROR", "redis", null, {
|
||||
companyId,
|
||||
error: error.message
|
||||
});
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Set Chatter API token in Redis cache
|
||||
* @param companyId
|
||||
* @param token
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
const setChatterToken = async (companyId, token) => {
|
||||
const key = getChatterTokenCacheKey(companyId);
|
||||
try {
|
||||
await pubClient.set(key, token);
|
||||
await pubClient.expire(key, CHATTER_TOKEN_CACHE_TTL);
|
||||
devDebugLogger("chatter-token-cache-set", {
|
||||
companyId,
|
||||
action: "Token cached"
|
||||
});
|
||||
} catch (error) {
|
||||
logger.log("set-chatter-token-in-redis", "ERROR", "redis", null, {
|
||||
companyId,
|
||||
error: error.message
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
const api = {
|
||||
getUserSocketMappingKey,
|
||||
getBodyshopCacheKey,
|
||||
getChatterTokenCacheKey,
|
||||
setSessionData,
|
||||
getSessionData,
|
||||
clearSessionData,
|
||||
@@ -390,7 +447,9 @@ const applyRedisHelpers = ({ pubClient, app, logger }) => {
|
||||
getSessionTransactionData,
|
||||
clearSessionTransactionData,
|
||||
setProviderCache,
|
||||
getProviderCache
|
||||
getProviderCache,
|
||||
getChatterToken,
|
||||
setChatterToken
|
||||
};
|
||||
|
||||
Object.assign(module.exports, api);
|
||||
|
||||
Reference in New Issue
Block a user