improve(api): cache salts

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-02-04 14:18:29 +01:00
parent 3a61276859
commit e3131ed72a
2 changed files with 14 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
import { generateSalt } from '@openpanel/common/server'; import { generateSalt } from '@openpanel/common/server';
import { db, getCurrentSalt } from '@openpanel/db'; import { db, getCurrentSalt } from '@openpanel/db';
import { getRedisCache } from '@openpanel/redis';
export async function salt() { export async function salt() {
const oldSalt = await getCurrentSalt().catch(() => null); const oldSalt = await getCurrentSalt().catch(() => null);
@@ -18,5 +19,7 @@ export async function salt() {
}, },
}); });
await getRedisCache().del('op:salt');
return newSalt; return newSalt;
} }

View File

@@ -1,5 +1,6 @@
import { generateSalt } from '@openpanel/common/server'; import { generateSalt } from '@openpanel/common/server';
import { getRedisCache } from '@openpanel/redis';
import { db } from '../prisma-client'; import { db } from '../prisma-client';
export async function getCurrentSalt() { export async function getCurrentSalt() {
@@ -17,6 +18,11 @@ export async function getCurrentSalt() {
} }
export async function getSalts() { export async function getSalts() {
const cache = await getRedisCache().get('op:salt');
if (cache) {
return JSON.parse(cache);
}
const [curr, prev] = await db.salt.findMany({ const [curr, prev] = await db.salt.findMany({
orderBy: { orderBy: {
createdAt: 'desc', createdAt: 'desc',
@@ -32,10 +38,14 @@ export async function getSalts() {
throw new Error('No salt found'); throw new Error('No salt found');
} }
return { const salts = {
current: curr.salt, current: curr.salt,
previous: prev.salt, previous: prev.salt,
}; };
await getRedisCache().set('op:salt', JSON.stringify(salts), 'EX', 60 * 10);
return salts;
} }
export async function createInitialSalts() { export async function createInitialSalts() {