Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

improve(api): cache salts

Carl-Gerhard Lindesvärde3131ed72a13
diff --git a/apps/worker/src/jobs/cron.salt.ts b/apps/worker/src/jobs/cron.salt.ts
index 617405c6f..f41598428 100644
--- a/apps/worker/src/jobs/cron.salt.ts
+++ b/apps/worker/src/jobs/cron.salt.ts
@@ -1,5 +1,6 @@
import { generateSalt } from '@openpanel/common/server';
import { db, getCurrentSalt } from '@openpanel/db';
+import { getRedisCache } from '@openpanel/redis';
export async function salt() {
const oldSalt = await getCurrentSalt().catch(() => null);
@@ -18,5 +19,7 @@ export async function salt() {
},
});
+ await getRedisCache().del('op:salt');
+
return newSalt;
}
diff --git a/packages/db/src/services/salt.service.ts b/packages/db/src/services/salt.service.ts
index 0bf6167de..c35134295 100644
--- a/packages/db/src/services/salt.service.ts
+++ b/packages/db/src/services/salt.service.ts
@@ -1,5 +1,6 @@
import { generateSalt } from '@openpanel/common/server';
+import { getRedisCache } from '@openpanel/redis';
import { db } from '../prisma-client';
export async function getCurrentSalt() {
@@ -17,6 +18,11 @@ export async function getCurrentSalt() {
}
export async function getSalts() {
+ const cache = await getRedisCache().get('op:salt');
+ if (cache) {
+ return JSON.parse(cache);
+ }
+
const [curr, prev] = await db.salt.findMany({
orderBy: {
createdAt: 'desc',
@@ -32,10 +38,14 @@ export async function getSalts() {
throw new Error('No salt found');
}
- return {
+ const salts = {
current: curr.salt,
previous: prev.salt,
};
+
+ await getRedisCache().set('op:salt', JSON.stringify(salts), 'EX', 60 * 10);
+
+ return salts;
}
export async function createInitialSalts() {