Skip to content

Commit

Permalink
feat: added redis to cache large queries (#190)
Browse files Browse the repository at this point in the history
feat: added redis to cache alrge queries
  • Loading branch information
wajeht authored Aug 29, 2022
1 parent fe01241 commit 2dbabe2
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 5 deletions.
178 changes: 174 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"fluent-ffmpeg": "^2.1.2",
"helmet": "^5.1.0",
"http-status-codes": "^2.2.0",
"ioredis": "^5.2.3",
"jsonwebtoken": "^8.5.1",
"knex": "^2.2.0",
"knex-paginate": "^3.0.1",
Expand Down
10 changes: 9 additions & 1 deletion src/apps/api/v1/sessions/sessions.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import logger from '../../../../utils/logger.js';
import { omit } from 'lodash-es';
import CustomError from '../../api.errors.js';
import db from '../../../../database/db.js';
import redis from '../../../../utils/redis.js';

/**
* It creates a session for a user
Expand All @@ -12,9 +13,11 @@ import db from '../../../../database/db.js';
*/
export async function postCreateSession(req, res) {
const body = req.body;
const { user_id } = req.body;
const created = await SessionQueries.createASession(body);

if (!created.length) throw new CustomError.BadRequestError(`Something went wrong while creating a session for for User ID: ${body.user_id}!`); // prettier-ignore
const deletedCacheSessions = await redis.del(`user-id-${user_id}-sessions`);

logger.info(`UserID: ${body.user_id} has created a SessionID: ${created[0].id}`);

Expand Down Expand Up @@ -91,7 +94,12 @@ export async function getUserSessions(req, res) {
currentPage: currentPage ?? null,
};

const sessions = await SessionQueries.getSessionsByUserId(user_id, pagination);
let sessions = JSON.parse(await redis.get(`user-id-${user_id}-sessions`));

if (sessions === null) {
sessions = await SessionQueries.getSessionsByUserId(user_id, pagination);
const setSessions = await redis.set(`user-id-${user_id}-sessions`, JSON.stringify(sessions));
}

// if (!sessions.data.length) throw new CustomError.BadRequestError(`There are no sessions available for user id ${user_id}!`); // prettier-ignore

Expand Down
9 changes: 9 additions & 0 deletions src/config/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,12 @@ export const admin = {
username: process.env.ADMIN_USERNAME,
password: process.env.ADMIN_PASSWORD,
};

/* Exporting the redis object. */
export const REDIS = {
port: process.env.REDIS_PORT,
host: process.env.REDIS_HOST,
username: process.env.REDIS_USERNAME,
password: process.env.REDIS_PASSWORD,
db: process.env.REDIS_DB,
};
23 changes: 23 additions & 0 deletions src/utils/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { REDIS } from '../config/env.js';
import Redis from 'ioredis';
import logger from './logger.js';
import Chad from './chad.js';

const client = new Redis({
port: REDIS.port, // Redis port
host: REDIS.host, // Redis host
username: REDIS.username, // needs Redis >= 6
password: REDIS.password,
db: REDIS.db, // Defaults to 0
});

client.on('error', (error) => {
logger.error(e);
Chad.flex(e.message, e.stack);
});

client.on('error', (error) => {
logger.info(`Redis client started`);
});

export default client;

0 comments on commit 2dbabe2

Please sign in to comment.