feature/IO-3701-Harness-Replacement - Implement
This commit is contained in:
@@ -7,6 +7,8 @@ const client = require("../graphql-client/graphql-client").client;
|
||||
* @type {number}
|
||||
*/
|
||||
const BODYSHOP_CACHE_TTL = 3600; // 1 hour
|
||||
const FEATURE_FLAGS_CACHE_TTL = 3600; // 1 hour
|
||||
const FEATURE_FLAGS_CACHE_VERSION_KEY = "bodyshop-feature-flags:version";
|
||||
|
||||
/**
|
||||
* Chatter API token cache TTL in seconds
|
||||
@@ -20,6 +22,7 @@ const CHATTER_TOKEN_CACHE_TTL = 3600; // 1 hour
|
||||
* @returns {`bodyshop-cache:${string}`}
|
||||
*/
|
||||
const getBodyshopCacheKey = (bodyshopId) => `bodyshop-cache:${bodyshopId}`;
|
||||
const getBodyshopFeatureFlagsCacheKey = (bodyshopId, version = "1") => `bodyshop-feature-flags:v${version}:${bodyshopId}`;
|
||||
|
||||
/**
|
||||
* Generate a cache key for a Chatter API token
|
||||
@@ -418,6 +421,92 @@ const applyRedisHelpers = ({ pubClient, app, logger }) => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads or initializes the global feature flag cache version used by per-shop keys.
|
||||
*/
|
||||
const getBodyshopFeatureFlagsCacheVersion = async () => {
|
||||
try {
|
||||
const version = await pubClient.get(FEATURE_FLAGS_CACHE_VERSION_KEY);
|
||||
if (version) return version;
|
||||
|
||||
await pubClient.setnx(FEATURE_FLAGS_CACHE_VERSION_KEY, "1");
|
||||
return "1";
|
||||
} catch (error) {
|
||||
logger.log("get-bodyshop-feature-flags-cache-version", "ERROR", "redis", null, {
|
||||
error: error.message
|
||||
});
|
||||
return "1";
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads a bodyshop feature flag payload from the current or supplied cache version.
|
||||
*/
|
||||
const getBodyshopFeatureFlagsFromRedis = async (bodyshopId, version) => {
|
||||
const cacheVersion = version || (await getBodyshopFeatureFlagsCacheVersion());
|
||||
const key = getBodyshopFeatureFlagsCacheKey(bodyshopId, cacheVersion);
|
||||
|
||||
try {
|
||||
const cachedData = await pubClient.get(key);
|
||||
return cachedData ? JSON.parse(cachedData) : null;
|
||||
} catch (error) {
|
||||
logger.log("get-bodyshop-feature-flags-from-redis", "ERROR", "redis", null, {
|
||||
bodyshopId,
|
||||
error: error.message
|
||||
});
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Stores a bodyshop feature flag payload under a versioned Redis key.
|
||||
*/
|
||||
const setBodyshopFeatureFlagsInRedis = async (bodyshopId, value, version) => {
|
||||
const cacheVersion = version || (await getBodyshopFeatureFlagsCacheVersion());
|
||||
const key = getBodyshopFeatureFlagsCacheKey(bodyshopId, cacheVersion);
|
||||
|
||||
try {
|
||||
await pubClient.set(key, toRedisJson(value));
|
||||
await pubClient.expire(key, FEATURE_FLAGS_CACHE_TTL);
|
||||
} catch (error) {
|
||||
logger.log("set-bodyshop-feature-flags-in-redis", "ERROR", "redis", null, {
|
||||
bodyshopId,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Deletes one bodyshop's feature flag cache entry for the current or supplied version.
|
||||
*/
|
||||
const invalidateBodyshopFeatureFlagsInRedis = async (bodyshopId, version) => {
|
||||
const cacheVersion = version || (await getBodyshopFeatureFlagsCacheVersion());
|
||||
const key = getBodyshopFeatureFlagsCacheKey(bodyshopId, cacheVersion);
|
||||
|
||||
try {
|
||||
await pubClient.del(key);
|
||||
} catch (error) {
|
||||
logger.log("invalidate-bodyshop-feature-flags-in-redis", "ERROR", "redis", null, {
|
||||
bodyshopId,
|
||||
error: error.message
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Invalidates all bodyshop feature flag caches by incrementing the global version.
|
||||
*/
|
||||
const invalidateAllBodyshopFeatureFlagsInRedis = async () => {
|
||||
try {
|
||||
return await pubClient.incr(FEATURE_FLAGS_CACHE_VERSION_KEY);
|
||||
} catch (error) {
|
||||
logger.log("invalidate-all-bodyshop-feature-flags-in-redis", "ERROR", "redis", null, {
|
||||
error: error.message
|
||||
});
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Set provider cache data
|
||||
* @param ns
|
||||
@@ -482,6 +571,7 @@ const applyRedisHelpers = ({ pubClient, app, logger }) => {
|
||||
const api = {
|
||||
getUserSocketMappingKey,
|
||||
getBodyshopCacheKey,
|
||||
getBodyshopFeatureFlagsCacheKey,
|
||||
getChatterTokenCacheKey,
|
||||
setSessionData,
|
||||
getSessionData,
|
||||
@@ -493,6 +583,11 @@ const applyRedisHelpers = ({ pubClient, app, logger }) => {
|
||||
refreshUserSocketTTL,
|
||||
getBodyshopFromRedis,
|
||||
updateOrInvalidateBodyshopFromRedis,
|
||||
getBodyshopFeatureFlagsCacheVersion,
|
||||
getBodyshopFeatureFlagsFromRedis,
|
||||
setBodyshopFeatureFlagsInRedis,
|
||||
invalidateBodyshopFeatureFlagsInRedis,
|
||||
invalidateAllBodyshopFeatureFlagsInRedis,
|
||||
setSessionTransactionData,
|
||||
getSessionTransactionData,
|
||||
clearSessionTransactionData,
|
||||
|
||||
Reference in New Issue
Block a user