Skip to content

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

perf(api): organization and notification rule lookups hit the in-memory cache

Carl-Gerhard Lindesvärd4d5ac9c6e8f4
diff --git a/packages/core/src/modules/notification/notification.service.ts b/packages/core/src/modules/notification/notification.service.ts
index ecc5ebf43..b53fb22a2 100644
--- a/packages/core/src/modules/notification/notification.service.ts
+++ b/packages/core/src/modules/notification/notification.service.ts
@@ -4,7 +4,7 @@
import type { Integration, Prisma } from '@openpanel/db/src/prisma-client';
import { stripLeadingAndTrailingSlashes } from '@openpanel/shared';
import { pathOr } from 'ramda';
-import { cacheablePerDeps } from '../../cacheable-per-deps';
+import { cacheablePerDb, type DbScope } from '../../cacheable-per-deps';
import { sendEmail } from '../../clients/email';
import { TRPCBadRequestError, TRPCForbiddenError } from '../../rpc/errors';
import type { ServiceDeps, Services } from '../../services';
@@ -64,9 +64,10 @@ export type INotificationRuleCached = Awaited<
ReturnType<typeof getNotificationRulesByProjectId>
>[number];
-export const getNotificationRulesByProjectId = cacheablePerDeps(
+// Per process, not per request: checked on every ingested event.
+export const getNotificationRulesByProjectId = cacheablePerDb(
'getNotificationRulesByProjectId',
- (deps: ServiceDeps, projectId: string) =>
+ (deps: DbScope, projectId: string) =>
deps.db.notificationRule.findMany({
where: { projectId },
select: {
diff --git a/packages/core/src/modules/organization/organization.service.ts b/packages/core/src/modules/organization/organization.service.ts
index 50fc447af..98cbe4b64 100644
--- a/packages/core/src/modules/organization/organization.service.ts
+++ b/packages/core/src/modules/organization/organization.service.ts
@@ -1,6 +1,5 @@
-// The two `cacheable` wrappers are `cacheablePerDeps`: `cacheable` keys on the
-// call's arguments, so the caller's deps travel beside the key rather than
-// inside it, or the Redis key would change per request.
+// `cacheable` keys on the call's arguments, so the caller's deps travel beside
+// the key rather than inside it, or the Redis key would change per request.
import { sql } from '@openpanel/db/src/clickhouse/sql';
import type {
@@ -10,7 +9,11 @@ import type {
User,
} from '@openpanel/db/src/prisma-client';
import { DateTime, generateSecureId } from '@openpanel/shared';
-import { cacheablePerDeps } from '../../cacheable-per-deps';
+import {
+ cacheablePerDb,
+ cacheablePerDeps,
+ type DbScope,
+} from '../../cacheable-per-deps';
import { chQuery } from '../../ch-query';
import { sendEmail } from '../../clients/email';
import { TRPCBadRequestError, TRPCNotFoundError } from '../../rpc/errors';
@@ -79,7 +82,7 @@ export async function getOrganizationById(
}
export async function getOrganizationByProjectId(
- deps: ServiceDeps,
+ deps: DbScope,
projectId: string
) {
const db = deps.db;
@@ -100,9 +103,10 @@ export async function getOrganizationByProjectId(
}
const ORGANIZATION_BY_PROJECT_CACHE_TTL_SEC = 60 * 5;
-export const getOrganizationByProjectIdCached = cacheablePerDeps(
+// Per process, not per request: ingest reads it on every event.
+export const getOrganizationByProjectIdCached = cacheablePerDb(
'getOrganizationByProjectId',
- (deps: ServiceDeps, projectId: string) =>
+ (deps: DbScope, projectId: string) =>
getOrganizationByProjectId(deps, projectId),
ORGANIZATION_BY_PROJECT_CACHE_TTL_SEC
);