configure redis

This commit is contained in:
Brian Cao 2022-08-29 10:47:01 -07:00
parent efb52f5ff1
commit 48fe6ebcc5
13 changed files with 138 additions and 183 deletions

View file

@ -34,7 +34,7 @@ function getClient() {
});
if (process.env.NODE_ENV !== 'production') {
global[CLICKHOUSE] = clickhouse;
global[CLICKHOUSE] = client;
}
log('Clickhouse initialized');

View file

@ -1,22 +1,14 @@
import { createClient } from 'redis';
import Redis from 'ioredis';
import { startOfMonth } from 'date-fns';
import debug from 'debug';
// import debug from 'debug';
import { getSessions, getAllWebsites } from 'queries';
import { REDIS } from 'lib/db';
const log = debug('umami:redis');
// const log = debug('umami:redis');
const INITIALIZED = 'redis:initialized';
async function getClient() {
const redis = new createClient({
url: process.env.REDIS_URL,
});
await redis.connect();
if (process.env.LOG_QUERY) {
redis.on('error', err => log(err));
}
function getClient() {
const redis = new Redis(process.env.REDIS_URL);
if (process.env.NODE_ENV !== 'production') {
global[REDIS] = redis;
@ -26,11 +18,11 @@ async function getClient() {
}
async function stageData() {
const sessions = await getSessions([], startOfMonth(new Date()).toUTCString());
const sessions = await getSessions([], startOfMonth(new Date()));
const websites = await getAllWebsites();
const sessionUuids = sessions.map(a => {
return { key: `session:${a.session_uuid}`, value: '' };
return { key: `session:${a.session_uuid}`, value: 1 };
});
const websiteIds = websites.map(a => {
return { key: `website:${a.website_uuid}`, value: Number(a.website_id) };
@ -50,16 +42,12 @@ async function addRedis(ids) {
}
// Initialization
let redis = null;
const redis = process.env.REDIS_URL && (global[REDIS] || getClient());
(async () => {
redis = process.env.REDIS_URL && (global[REDIS] || (await getClient()));
if (redis) {
if (!(await redis.get(INITIALIZED))) {
await stageData();
}
if (!(await redis.get(INITIALIZED))) {
await stageData();
}
})();
export default redis;
export default { client: redis, stageData };

View file

@ -30,9 +30,11 @@ export async function getSession(req) {
let websiteId = null;
//console.log(await redis.stageData());
// Check if website exists
if (process.env.REDIS_URL) {
websiteId = await redis.get(`website:${website_uuid}`);
websiteId = await redis.client.get(`website:${website_uuid}`);
} else {
const { website_id } = await getWebsiteByUuid(website_uuid);
websiteId = website_id;
@ -52,7 +54,7 @@ export async function getSession(req) {
// Check if session exists
if (process.env.REDIS_URL) {
sessionCreated = (await redis.get(`session:${session_uuid}`)) !== null;
sessionCreated = !!(await redis.client.get(`session:${session_uuid}`));
} else {
session = await getSessionByUuid(session_uuid);
sessionCreated = !!session;
@ -61,7 +63,7 @@ export async function getSession(req) {
if (!sessionCreated) {
try {
session = await createSession(websiteId, {
session = await createSession(BigInt(websiteId), {
session_uuid,
hostname,
browser,