Updated redis calls.

This commit is contained in:
Mike Cao 2024-12-11 19:43:23 -08:00
parent bb5affe29a
commit 62a8b29453
9 changed files with 41 additions and 27 deletions

View File

@ -77,7 +77,7 @@
"@react-spring/web": "^9.7.3",
"@tanstack/react-query": "^5.28.6",
"@umami/prisma-client": "^0.14.0",
"@umami/redis-client": "^0.21.0",
"@umami/redis-client": "^0.24.0",
"chalk": "^4.1.1",
"chart.js": "^4.4.2",
"chartjs-adapter-date-fns": "^3.0.0",

View File

@ -1,7 +1,7 @@
import { Report } from '@prisma/client';
import redis from '@umami/redis-client';
import { getClient } from '@umami/redis-client';
import debug from 'debug';
import { PERMISSIONS, ROLE_PERMISSIONS, SHARE_TOKEN_HEADER, ROLES } from 'lib/constants';
import { PERMISSIONS, ROLE_PERMISSIONS, SHARE_TOKEN_HEADER } from 'lib/constants';
import { secret } from 'lib/crypto';
import { NextApiRequest } from 'next';
import { createSecureToken, ensureArray, getRandomChars, parseToken } from 'next-basics';
@ -14,10 +14,12 @@ const cloudMode = process.env.CLOUD_MODE;
export async function saveAuth(data: any, expire = 0) {
const authKey = `auth:${getRandomChars(32)}`;
await redis.client.set(authKey, data);
const redis = getClient();
await redis.set(authKey, data);
if (expire) {
await redis.client.expire(authKey, expire);
await redis.expire(authKey, expire);
}
return createSecureToken({ authKey }, secret());

View File

@ -1,12 +1,14 @@
import { getWebsiteSession, getWebsite } from 'queries';
import { Website, Session } from '@prisma/client';
import redis from '@umami/redis-client';
import { getClient, redisEnabled } from '@umami/redis-client';
export async function fetchWebsite(websiteId: string): Promise<Website> {
let website = null;
if (redis.enabled) {
website = await redis.client.fetch(`website:${websiteId}`, () => getWebsite(websiteId), 86400);
if (redisEnabled) {
const redis = getClient();
website = await redis.fetch(`website:${websiteId}`, () => getWebsite(websiteId), 86400);
} else {
website = await getWebsite(websiteId);
}
@ -21,8 +23,10 @@ export async function fetchWebsite(websiteId: string): Promise<Website> {
export async function fetchSession(websiteId: string, sessionId: string): Promise<Session> {
let session = null;
if (redis.enabled) {
session = await redis.client.fetch(
if (redisEnabled) {
const redis = getClient();
session = await redis.fetch(
`session:${sessionId}`,
() => getWebsiteSession(websiteId, sessionId),
86400,

View File

@ -1,6 +1,6 @@
import cors from 'cors';
import debug from 'debug';
import redis from '@umami/redis-client';
import { getClient, redisEnabled } from '@umami/redis-client';
import { getAuthToken, parseShareToken } from 'lib/auth';
import { ROLES } from 'lib/constants';
import { secret } from 'lib/crypto';
@ -54,8 +54,10 @@ export const useAuth = createMiddleware(async (req, res, next) => {
if (userId) {
user = await getUser(userId);
} else if (redis.enabled && authKey) {
const key = await redis.client.get(authKey);
} else if (redisEnabled && authKey) {
const redis = getClient();
const key = await redis.get(authKey);
if (key?.userId) {
user = await getUser(key.userId);

View File

@ -1,4 +1,4 @@
import redis from '@umami/redis-client';
import { redisEnabled } from '@umami/redis-client';
import { saveAuth } from 'lib/auth';
import { secret } from 'lib/crypto';
import { useValidate } from 'lib/middleware';
@ -49,7 +49,7 @@ export default async (
const user = await getUserByUsername(username, { includePassword: true });
if (user && checkPassword(password, user.password)) {
if (redis.enabled) {
if (redisEnabled) {
const token = await saveAuth({ userId: user.id });
return ok(res, { token, user });

View File

@ -1,5 +1,5 @@
import { methodNotAllowed, ok } from 'next-basics';
import redis from '@umami/redis-client';
import { getClient, redisEnabled } from '@umami/redis-client';
import { useAuth } from 'lib/middleware';
import { getAuthToken } from 'lib/auth';
import { NextApiRequest, NextApiResponse } from 'next';
@ -8,8 +8,10 @@ export default async (req: NextApiRequest, res: NextApiResponse) => {
await useAuth(req, res);
if (req.method === 'POST') {
if (redis.enabled) {
await redis.client.del(getAuthToken(req));
if (redisEnabled) {
const redis = getClient();
await redis.del(getAuthToken(req));
}
return ok(res);

View File

@ -2,13 +2,13 @@ import { NextApiRequestAuth } from 'lib/types';
import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { badRequest, ok } from 'next-basics';
import redis from '@umami/redis-client';
import { redisEnabled } from '@umami/redis-client';
import { saveAuth } from 'lib/auth';
export default async (req: NextApiRequestAuth, res: NextApiResponse) => {
await useAuth(req, res);
if (redis.enabled && req.auth.user) {
if (redisEnabled && req.auth.user) {
const token = await saveAuth({ userId: req.auth.user.id }, 86400);
return ok(res, { user: req.auth.user, token });

View File

@ -1,5 +1,5 @@
import { Prisma, Website } from '@prisma/client';
import redis from '@umami/redis-client';
import { getClient } from '@umami/redis-client';
import prisma from 'lib/prisma';
import { PageResult, PageParams } from 'lib/types';
import WebsiteFindManyArgs = Prisma.WebsiteFindManyArgs;
@ -182,7 +182,9 @@ export async function resetWebsite(
}),
]).then(async data => {
if (cloudMode) {
await redis.client.set(`website:${websiteId}`, data[3]);
const redis = getClient();
await redis.set(`website:${websiteId}`, data[3]);
}
return data;
@ -225,7 +227,9 @@ export async function deleteWebsite(
}),
]).then(async data => {
if (cloudMode) {
await redis.client.del(`website:${websiteId}`);
const redis = getClient();
await redis.del(`website:${websiteId}`);
}
return data;

View File

@ -3326,10 +3326,10 @@
chalk "^4.1.2"
debug "^4.3.4"
"@umami/redis-client@^0.21.0":
version "0.21.0"
resolved "https://registry.yarnpkg.com/@umami/redis-client/-/redis-client-0.21.0.tgz#96426b28860b8b06fae8825fc2f2d9575b64e863"
integrity sha512-PpdJunvT4sAsVWIeEl+cHU6iSV2r/Df9dof2gdUwSigfD88ACsVs1/BvlWERxk/T93rTgVJWSpLvdw/oMYvkcw==
"@umami/redis-client@^0.24.0":
version "0.24.0"
resolved "https://registry.yarnpkg.com/@umami/redis-client/-/redis-client-0.24.0.tgz#8af489250396be76bc0906766343620589774c4b"
integrity sha512-yUZmC87H5QZKNA6jD9k/7d8WDaXQTDROlpyK7S+V2csD96eAnMNi7JsWAVWx9T/584QKD8DsSIy87PTWq1HNPw==
dependencies:
debug "^4.3.4"
redis "^4.5.1"