mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
Refactored redis usage. Added lib/cache.
This commit is contained in:
parent
3485b6268b
commit
f118bc95c1
22 changed files with 236 additions and 221 deletions
108
lib/session.js
108
lib/session.js
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue