Skip to content

Commit

Permalink
simplified translation caching
Browse files Browse the repository at this point in the history
- removed redundant /cached redis key
- we can tell whether the translates are cached based on the locale key itself
  • Loading branch information
grossmannmartin committed Dec 15, 2023
1 parent 205491b commit 5017fd8
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions storefront/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { captureException } = require('@sentry/nextjs');
const REDIS_URL = `redis://${process.env.REDIS_HOST}`;
const REDIS_PREFIX = `${process.env.REDIS_PREFIX}:fe:translates:`;
const REDIS_UPDATE_JOB_TIMEOUT = 5; // seconds (default: 30)
const REDIS_IS_CACHED_TIMEOUT = 86400; // all day long (cache is invalidated on translation change on backend)

const logException = (e) => {
if (process.env.APP_ENV === 'development') {
Expand Down Expand Up @@ -47,13 +46,12 @@ module.exports = {

await redisClient.connect();

const [cachedTranslates, isCached, updateJobIsRunning] = await redisClient.mGet([
const [cachedTranslates, updateJobIsRunning] = await redisClient.mGet([
redisKey,
redisKey + '/cached',
redisKey + '/updating',
]);

if (isCached === null && updateJobIsRunning === null) {
if (cachedTranslates === null && updateJobIsRunning === null) {
const cacheToRedis = async () => {
const setUpdatingFlag = await redisClient.set(redisKey + '/updating', 'true', {
NX: true,
Expand All @@ -66,12 +64,7 @@ module.exports = {
const translatesToCache = JSON.stringify(freshTranslates);

if (translatesToCache) {
await Promise.all([
redisClient.set(redisKey, translatesToCache),
redisClient.set(redisKey + '/cached', 'true', {
EX: REDIS_IS_CACHED_TIMEOUT,
}),
]);
await Promise.all([redisClient.set(redisKey, translatesToCache)]);
}
}
};
Expand Down

0 comments on commit 5017fd8

Please sign in to comment.