Skip to content

Commit

Permalink
Merge pull request #3 from UNICEFECAR/feature/US-387-cache-client-data
Browse files Browse the repository at this point in the history
FEATURE: Cache User + Client data
  • Loading branch information
georgipavlov-7DIGIT authored Jan 19, 2023
2 parents 33c62c1 + b017a97 commit 33ea125
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 28 deletions.
4 changes: 3 additions & 1 deletion service/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ PORT=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=
AWS_BUCKET_NAME=
AWS_BUCKET_NAME=

REDIS_CACHE=
34 changes: 29 additions & 5 deletions service/controllers/clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "#queries/clients";

import { clientNotFound, incorrectPassword, emailUsed } from "#utils/errors";
import { deleteCacheItem } from "#utils/cache";

const AWS_ACCESS_KEY_ID = process.env.AWS_ACCESS_KEY_ID;
const AWS_SECRET_ACCESS_KEY = process.env.AWS_SECRET_ACCESS_KEY;
Expand Down Expand Up @@ -40,6 +41,7 @@ export const updateClientData = async ({
country,
language,
client_id,
user_id,
name,
surname,
nickname,
Expand Down Expand Up @@ -77,10 +79,13 @@ export const updateClientData = async ({
yearOfBirth,
urbanRural,
})
.then((res) => {
.then(async (res) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
Expand Down Expand Up @@ -145,6 +150,9 @@ export const deleteClientData = async ({
}
}

const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
Expand All @@ -157,17 +165,21 @@ export const updateClientImage = async ({
country,
language,
client_id,
user_id,
image,
}) => {
return await updateClientImageQuery({
poolCountry: country,
client_id,
image,
})
.then((res) => {
.then(async (res) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
Expand All @@ -176,15 +188,23 @@ export const updateClientImage = async ({
});
};

export const deleteClientImage = async ({ country, language, client_id }) => {
export const deleteClientImage = async ({
country,
language,
client_id,
user_id,
}) => {
return await deleteClientImageQuery({
poolCountry: country,
client_id,
})
.then((res) => {
.then(async (res) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
Expand All @@ -197,17 +217,21 @@ export const updateClientDataProcessing = async ({
country,
language,
client_id,
user_id,
dataProcessing,
}) => {
return await updateClientDataProcessingQuery({
poolCountry: country,
client_id,
dataProcessing,
})
.then((res) => {
.then(async (res) => {
if (res.rowCount === 0) {
throw clientNotFound(language);
} else {
const cacheKey = `client_${country}_${user_id}`;
await deleteCacheItem(cacheKey);

return res.rows[0];
}
})
Expand Down
45 changes: 30 additions & 15 deletions service/middlewares/populateMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,30 @@ import { getClientByUserID } from "#queries/clients";
import { getUserByID } from "#queries/users";

import { clientNotFound } from "#utils/errors";
import { getCacheItem, setCacheItem } from "#utils/cache";

export const populateClient = async (req, res, next) => {
const country = req.header("x-country-alpha-2");
const user_id = req.header("x-user-id");

const client = await getClientByUserID(country, user_id)
.then((res) => res.rows[0])
.catch((err) => {
throw err;
});
const cacheKey = `client_${country}_${user_id}`;
const cachedClientData = await getCacheItem(cacheKey);

if (!client) {
return next(clientNotFound(country));
}
if (cachedClientData) req.client = cachedClientData;
else {
const client = await getClientByUserID(country, user_id)
.then((res) => res.rows[0])
.catch((err) => {
throw err;
});

if (!client) {
return next(clientNotFound(country));
}

req.client = client;
await setCacheItem(cacheKey, client, 60 * 60 * 2); // cache data for 2 hours
req.client = client;
}

return next();
};
Expand All @@ -26,13 +34,20 @@ export const populateUser = async (req, res, next) => {
const country = req.header("x-country-alpha-2");
const user_id = req.header("x-user-id");

const user = await getUserByID(country, user_id)
.then((res) => res.rows[0])
.catch((err) => {
throw err;
});
const cacheKey = `user_${country}_${user_id}`;
const cachedUserData = await getCacheItem(cacheKey);

req.user = user;
if (cachedUserData) req.user = cachedUserData;
else {
const user = await getUserByID(country, user_id)
.then((res) => res.rows[0])
.catch((err) => {
throw err;
});

await setCacheItem(cacheKey, user, 60 * 60 * 2); // cache data for 2 hours
req.user = user;
}

return next();
};
Loading

0 comments on commit 33ea125

Please sign in to comment.