From 0fc8828f8b9d00d5860deaeb30035cf77c518aa8 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Fri, 26 Apr 2024 00:31:38 -0700 Subject: [PATCH] Refactored caching logic. --- src/lib/cache.ts | 73 ------------------- src/lib/clickhouse.ts | 4 +- src/lib/load.ts | 34 +++------ src/lib/prisma.ts | 4 +- src/lib/session.ts | 6 +- src/queries/admin/user.ts | 13 +--- .../analytics/sessions/createSession.ts | 41 ++++------- 7 files changed, 33 insertions(+), 142 deletions(-) delete mode 100644 src/lib/cache.ts diff --git a/src/lib/cache.ts b/src/lib/cache.ts deleted file mode 100644 index cb218ba0..00000000 --- a/src/lib/cache.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { User, Website } from '@prisma/client'; -import redis from '@umami/redis-client'; -import { getSession, getUser, getWebsite } from '../queries'; - -async function fetchWebsite(websiteId: string): Promise { - return redis.client.fetch(`website:${websiteId}`, () => getWebsite(websiteId), 86400); -} - -async function storeWebsite(data: { id: any }) { - const { id } = data; - const key = `website:${id}`; - - return redis.client.store(key, data, 86400); -} - -async function deleteWebsite(id: any) { - return redis.client.remove(`website:${id}`); -} - -async function fetchUser(id: string): Promise { - return redis.client.fetch(`user:${id}`, () => getUser(id, { includePassword: true }), 86400); -} - -async function storeUser(data: { id: any }) { - const { id } = data; - const key = `user:${id}`; - - return redis.client.store(key, data, 86400); -} - -async function deleteUser(id: any) { - return redis.client.remove(`user:${id}`); -} - -async function fetchSession(id: string) { - return redis.client.fetch(`session:${id}`, () => getSession(id), 86400); -} - -async function storeSession(data: { id: any }) { - const { id } = data; - const key = `session:${id}`; - - return redis.client.store(key, data, 86400); -} - -async function deleteSession(id: any) { - return redis.client.remove(`session:${id}`); -} - -async function fetchUserBlock(userId: string) { - const key = `user:block:${userId}`; - return redis.client.get(key); -} - -async function incrementUserBlock(userId: string) { - const key = `user:block:${userId}`; - return redis.client.incr(key); -} - -export default { - fetchWebsite, - storeWebsite, - deleteWebsite, - fetchUser, - storeUser, - deleteUser, - fetchSession, - storeSession, - deleteSession, - fetchUserBlock, - incrementUserBlock, - enabled: !!redis.enabled, -}; diff --git a/src/lib/clickhouse.ts b/src/lib/clickhouse.ts index 5bcf7ee0..c1b9d25f 100644 --- a/src/lib/clickhouse.ts +++ b/src/lib/clickhouse.ts @@ -4,7 +4,7 @@ import debug from 'debug'; import { CLICKHOUSE } from 'lib/db'; import { QueryFilters, QueryOptions } from './types'; import { OPERATORS } from './constants'; -import { loadWebsite } from './load'; +import { fetchWebsite } from './load'; import { maxDate } from './date'; import { filtersToArray } from './params'; @@ -106,7 +106,7 @@ function getFilterParams(filters: QueryFilters = {}) { } async function parseFilters(websiteId: string, filters: QueryFilters = {}, options?: QueryOptions) { - const website = await loadWebsite(websiteId); + const website = await fetchWebsite(websiteId); return { filterQuery: getFilterQuery(filters, options), diff --git a/src/lib/load.ts b/src/lib/load.ts index 78c0ac9c..c0a87897 100644 --- a/src/lib/load.ts +++ b/src/lib/load.ts @@ -1,12 +1,12 @@ -import cache from 'lib/cache'; -import { getSession, getUser, getWebsite } from 'queries'; -import { User, Website, Session } from '@prisma/client'; +import { getSession, getWebsite } from 'queries'; +import { Website, Session } from '@prisma/client'; +import redis from '@umami/redis-client'; -export async function loadWebsite(websiteId: string): Promise { +export async function fetchWebsite(websiteId: string): Promise { let website; - if (cache.enabled) { - website = await cache.fetchWebsite(websiteId); + if (redis.enabled) { + website = await redis.client.fetch(`website:${websiteId}`, () => getWebsite(websiteId), 86400); } else { website = await getWebsite(websiteId); } @@ -18,11 +18,11 @@ export async function loadWebsite(websiteId: string): Promise { return website; } -export async function loadSession(sessionId: string): Promise { +export async function fetchSession(sessionId: string): Promise { let session; - if (cache.enabled) { - session = await cache.fetchSession(sessionId); + if (redis.enabled) { + session = await redis.client.fetch(`session:${sessionId}`, () => getSession(sessionId), 86400); } else { session = await getSession(sessionId); } @@ -33,19 +33,3 @@ export async function loadSession(sessionId: string): Promise { return session; } - -export async function loadUser(userId: string): Promise { - let user; - - if (cache.enabled) { - user = await cache.fetchUser(userId); - } else { - user = await getUser(userId); - } - - if (!user || user.deletedAt) { - return null; - } - - return user; -} diff --git a/src/lib/prisma.ts b/src/lib/prisma.ts index c5e16ddc..eb866899 100644 --- a/src/lib/prisma.ts +++ b/src/lib/prisma.ts @@ -3,7 +3,7 @@ import prisma from '@umami/prisma-client'; import moment from 'moment-timezone'; import { MYSQL, POSTGRESQL, getDatabaseType } from 'lib/db'; import { SESSION_COLUMNS, OPERATORS, DEFAULT_PAGE_SIZE } from './constants'; -import { loadWebsite } from './load'; +import { fetchWebsite } from './load'; import { maxDate } from './date'; import { QueryFilters, QueryOptions, SearchFilter } from './types'; import { filtersToArray } from './params'; @@ -152,7 +152,7 @@ async function parseFilters( filters: QueryFilters = {}, options: QueryOptions = {}, ) { - const website = await loadWebsite(websiteId); + const website = await fetchWebsite(websiteId); const joinSession = Object.keys(filters).find(key => SESSION_COLUMNS.includes(key)); return { diff --git a/src/lib/session.ts b/src/lib/session.ts index b675ec2f..cf30aa71 100644 --- a/src/lib/session.ts +++ b/src/lib/session.ts @@ -4,7 +4,7 @@ import { parseToken } from 'next-basics'; import { NextApiRequestCollect } from 'pages/api/send'; import { createSession } from 'queries'; import clickhouse from './clickhouse'; -import { loadSession, loadWebsite } from './load'; +import { fetchSession, fetchWebsite } from './load'; import { SessionData } from 'lib/types'; export async function getSession(req: NextApiRequestCollect): Promise { @@ -30,7 +30,7 @@ export async function getSession(req: NextApiRequestCollect): Promise { - if (cache.enabled) { - const ids = websites.map(a => a.id); - - for (let i = 0; i < ids.length; i++) { - await cache.deleteWebsite(`website:${ids[i]}`); - } - } - - return data; - }); + ]); } diff --git a/src/queries/analytics/sessions/createSession.ts b/src/queries/analytics/sessions/createSession.ts index 65dbd794..7d614499 100644 --- a/src/queries/analytics/sessions/createSession.ts +++ b/src/queries/analytics/sessions/createSession.ts @@ -1,5 +1,4 @@ import { Prisma } from '@prisma/client'; -import cache from 'lib/cache'; import prisma from 'lib/prisma'; export async function createSession(data: Prisma.SessionCreateInput) { @@ -18,28 +17,20 @@ export async function createSession(data: Prisma.SessionCreateInput) { city, } = data; - return prisma.client.session - .create({ - data: { - id, - websiteId, - hostname, - browser, - os, - device, - screen, - language, - country, - subdivision1, - subdivision2, - city, - }, - }) - .then(async data => { - if (cache.enabled) { - await cache.storeSession(data); - } - - return data; - }); + return prisma.client.session.create({ + data: { + id, + websiteId, + hostname, + browser, + os, + device, + screen, + language, + country, + subdivision1, + subdivision2, + city, + }, + }); }