Skip to content

Commit

Permalink
feat: cached users queries
Browse files Browse the repository at this point in the history
  • Loading branch information
wajeht committed Oct 11, 2022
1 parent c826a18 commit 49938e5
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/apps/api/v1/users/users.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,26 @@ export async function postUser(req, res) {
*/
export async function getUsers(req, res) {
const { email } = req.query;

const cachedUsers = JSON.parse(await redis.get(`user-id-${req.user.user_id}-users`));

let users;

if (email) {
users = await UsersQueries.getAllUsers(email);
} else {
users = await UsersQueries.getAllUsers();
if (cachedUsers === null) {
if (email) {
users = await UsersQueries.getAllUsers(email);
} else {
users = await UsersQueries.getAllUsers();
}

await redis.set(`user-id-${req.user.user_id}-users`, JSON.stringify(users));
}

res.status(StatusCodes.OK).json({
status: 'success',
request_url: req.originalUrl,
message: 'The resource was returned successfully!',
data: users,
data: cachedUsers,
});
}

Expand Down

0 comments on commit 49938e5

Please sign in to comment.