Refactored redis usage. Added lib/cache.

This commit is contained in:
Mike Cao 2022-11-07 22:35:51 -08:00
parent 3485b6268b
commit f118bc95c1
22 changed files with 236 additions and 221 deletions

View file

@ -0,0 +1,84 @@
import { getWebsite, getUser, getSession } from '../queries';
import redis, { DELETED } from 'lib/redis';
async function fetchObject(key, query) {
const obj = await redis.get(key);
if (!obj) {
return query().then(async data => {
if (data) {
await redis.set(key, data);
}
return data;
});
}
return obj;
}
async function storeObject(key, data) {
return redis.set(key, data);
}
async function deleteObject(key) {
return redis.set(key, DELETED);
}
async function fetchWebsite(id) {
return fetchObject(`website:${id}`, () => getWebsite({ id }));
}
async function storeWebsite(data) {
const { id } = data;
const key = `website:${id}`;
return storeObject(key, data);
}
async function deleteWebsite(id) {
return deleteObject(`website:${id}`);
}
async function fetchUser(id) {
return fetchObject(`user:${id}`, () => getUser({ id }));
}
async function storeUser(data) {
const { id } = data;
const key = `user:${id}`;
return storeObject(key, data);
}
async function deleteUser(id) {
return deleteObject(`user:${id}`);
}
async function fetchSession(id) {
return fetchObject(`session:${id}`, () => getSession({ id }));
}
async function storeSession(data) {
const { id } = data;
const key = `session:${id}`;
return storeObject(key, data);
}
async function deleteSession(id) {
return deleteObject(`session:${id}`);
}
export default {
fetchWebsite,
storeWebsite,
deleteWebsite,
fetchUser,
storeUser,
deleteUser,
fetchSession,
storeSession,
deleteSession,
enabled: redis.enabled,
};

View file

@ -1,23 +1,19 @@
import { createMiddleware, unauthorized, badRequest, serverError } from 'next-basics';
import { createMiddleware, unauthorized, badRequest } from 'next-basics';
import debug from 'debug';
import cors from 'cors';
import { getSession } from './session';
import { parseAuthToken, parseShareToken } from './auth';
import { findSession } from 'lib/session';
import { parseAuthToken, parseShareToken } from 'lib/auth';
import redis from 'lib/redis';
const log = debug('umami:middleware');
export const useCors = createMiddleware(cors());
export const useSession = createMiddleware(async (req, res, next) => {
let session;
try {
session = await getSession(req);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
return serverError(res, e.message);
}
const session = await findSession(req);
if (!session) {
log('useSession:session-not-found');
return badRequest(res);
}
@ -29,10 +25,14 @@ export const useAuth = createMiddleware(async (req, res, next) => {
const token = await parseAuthToken(req);
const shareToken = await parseShareToken(req);
if (!token && !shareToken) {
const key = `auth:${token?.authKey}`;
const data = redis.enabled ? await redis.get(key) : token;
if (!data && !shareToken) {
log('useAuth:user-not-authorized');
return unauthorized(res);
}
req.auth = { ...token, shareToken };
req.auth = { ...data, shareToken };
next();
});

View file

@ -32,13 +32,13 @@ function getClient() {
async function get(key) {
await connect();
return redis.get(key);
return JSON.parse(await redis.get(key));
}
async function set(key, value) {
await connect();
return redis.set(key, value);
return redis.set(key, JSON.stringify(value));
}
async function del(key) {

View file

@ -1,102 +1,80 @@
import { parseToken } from 'next-basics';
import { validate } from 'uuid';
import { secret, uuid } from 'lib/crypto';
import redis, { DELETED } from 'lib/redis';
import clickhouse from 'lib/clickhouse';
import cache from 'lib/cache';
import { getClientInfo, getJsonBody } from 'lib/request';
import { createSession, getSession as getSessionPrisma, getWebsite } from 'queries';
import { createSession, getSession, getWebsite } from 'queries';
export async function getSession(req) {
export async function findSession(req) {
const { payload } = getJsonBody(req);
if (!payload) {
throw new Error('Invalid request');
return null;
}
const cache = req.headers['x-umami-cache'];
// Check if cache token is passed
const cacheToken = req.headers['x-umami-cache'];
if (cache) {
const result = await parseToken(cache, secret());
if (cacheToken) {
const result = await parseToken(cacheToken, secret());
if (result) {
return result;
}
}
// Verify payload
const { website: websiteId, hostname, screen, language } = payload;
if (!validate(websiteId)) {
return null;
}
let isValidWebsite = null;
// Find website
let website;
// Check if website exists
if (redis.enabled) {
isValidWebsite = await redis.get(`website:${websiteId}`);
if (cache.enabled) {
website = await cache.fetchWebsite(websiteId);
} else {
website = await getWebsite({ id: websiteId });
}
// Check database if does not exists in Redis
if (!isValidWebsite) {
const website = await getWebsite({ id: websiteId });
isValidWebsite = !!website;
}
if (!isValidWebsite || isValidWebsite === DELETED) {
if (!website || website.isDeleted) {
throw new Error(`Website not found: ${websiteId}`);
}
const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
const sessionId = uuid(websiteId, hostname, ip, userAgent);
let isValidSession = null;
let session = null;
// Find session
let session;
if (!clickhouse.enabled) {
// Check if session exists
if (redis.enabled) {
isValidSession = await redis.get(`session:${sessionId}`);
}
// Check database if does not exists in Redis
if (!isValidSession) {
session = await getSessionPrisma({ id: sessionId });
isValidSession = !!session;
}
if (!isValidSession) {
try {
session = await createSession(websiteId, {
id: sessionId,
hostname,
browser,
os,
screen,
language,
country,
device,
});
} catch (e) {
if (!e.message.toLowerCase().includes('unique constraint')) {
throw e;
}
}
}
if (cache.enabled) {
session = await cache.fetchSession(sessionId);
} else {
session = {
id: sessionId,
hostname,
browser,
os,
screen,
language,
country,
device,
};
session = await getSession({ id: sessionId });
}
return {
websiteId,
session,
};
// Create a session if not found
if (!session) {
try {
session = await createSession({
id: sessionId,
websiteId,
hostname,
browser,
os,
device,
screen,
language,
country,
});
} catch (e) {
if (!e.message.toLowerCase().includes('unique constraint')) {
throw e;
}
}
}
return session;
}