Skip to content

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

fix: cache empty notifications rules (reduce pg tps)

Carl-Gerhard Lindesvärd8c1636d01c93
diff --git a/packages/db/src/services/notification.service.ts b/packages/db/src/services/notification.service.ts
index b1cbc1a50..5b357151a 100644
--- a/packages/db/src/services/notification.service.ts
+++ b/packages/db/src/services/notification.service.ts
@@ -91,6 +91,7 @@ export const getNotificationRulesByProjectId = cacheable(
});
},
60 * 24,
+ { cacheEmptyArray: true }
);
function getIntegration(integrationId: string | null) {
diff --git a/packages/redis/cachable.test.ts b/packages/redis/cachable.test.ts
index 2d212a2ea..a9489679a 100644
--- a/packages/redis/cachable.test.ts
+++ b/packages/redis/cachable.test.ts
@@ -1,3 +1,4 @@
+/** biome-ignore-all lint/correctness/noUnusedFunctionParameters: test */
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { cacheable, getCache } from './cachable';
import { getRedisCache } from './redis';
@@ -9,8 +10,8 @@ describe('cachable', () => {
redis = getRedisCache();
// Clear any existing cache data for clean tests
const keys = [
- ...await redis.keys('cachable:*'),
- ...await redis.keys('test-key*'),
+ ...(await redis.keys('cachable:*')),
+ ...(await redis.keys('test-key*')),
];
if (keys.length > 0) {
await redis.del(...keys);
@@ -20,8 +21,8 @@ describe('cachable', () => {
afterEach(async () => {
// Clean up after each test
const keys = [
- ...await redis.keys('cachable:*'),
- ...await redis.keys('test-key*'),
+ ...(await redis.keys('cachable:*')),
+ ...(await redis.keys('test-key*')),
];
if (keys.length > 0) {
await redis.del(...keys);
@@ -139,7 +140,7 @@ describe('cachable', () => {
const cachedFn = cacheable(
'testFunction',
async (arg1: string, arg2: string) => mockData,
- 3600,
+ 3600
);
await cachedFn('arg1', 'arg2');
@@ -233,6 +234,106 @@ describe('cachable', () => {
expect(cached).toBeNull();
});
+ it('should cache empty arrays when cacheEmptyArray option is enabled', async () => {
+ let callCount = 0;
+ const fn = async (arg1: string) => {
+ callCount++;
+ return [] as number[];
+ };
+
+ const cachedFn = cacheable(fn, 3600, { cacheEmptyArray: true });
+ const first = await cachedFn('arg1');
+
+ expect(first).toEqual([]);
+ expect(callCount).toBe(1);
+
+ // Verify it was written to Redis
+ const key = cachedFn.getKey('arg1');
+ const cached = await redis.get(key);
+ expect(cached).toBe('[]');
+
+ // Second call should hit the cache (L1 LRU) and not invoke the function
+ const second = await cachedFn('arg1');
+ expect(second).toEqual([]);
+ expect(callCount).toBe(1);
+ });
+
+ it('should serve cached empty array from Redis on a fresh instance (no L1)', async () => {
+ let firstCallCount = 0;
+ const firstFn = async (arg1: string) => {
+ firstCallCount++;
+ return [] as number[];
+ };
+
+ // First instance writes the empty array into Redis
+ const firstCachedFn = cacheable('sharedEmptyArrayFn', firstFn, 3600, {
+ cacheEmptyArray: true,
+ });
+ await firstCachedFn('arg1');
+ expect(firstCallCount).toBe(1);
+
+ // Second instance has a cold L1 LRU; it must read [] from Redis
+ let secondCallCount = 0;
+ const secondFn = async (arg1: string) => {
+ secondCallCount++;
+ return [1, 2, 3];
+ };
+ const secondCachedFn = cacheable('sharedEmptyArrayFn', secondFn, 3600, {
+ cacheEmptyArray: true,
+ });
+ const result = await secondCachedFn('arg1');
+
+ expect(result).toEqual([]);
+ expect(secondCallCount).toBe(0);
+ });
+
+ it('should still not cache null even with cacheEmptyArray enabled', async () => {
+ let callCount = 0;
+ const fn = async (arg1: string) => {
+ callCount++;
+ return null;
+ };
+
+ const cachedFn = cacheable(fn, 3600, { cacheEmptyArray: true });
+ await cachedFn('arg1');
+ await cachedFn('arg1');
+
+ expect(callCount).toBe(2);
+ const key = cachedFn.getKey('arg1');
+ const cached = await redis.get(key);
+ expect(cached).toBeNull();
+ });
+
+ it('should still not cache empty objects even with cacheEmptyArray enabled', async () => {
+ let callCount = 0;
+ const fn = async (arg1: string) => {
+ callCount++;
+ return {};
+ };
+
+ const cachedFn = cacheable(fn, 3600, { cacheEmptyArray: true });
+ await cachedFn('arg1');
+ await cachedFn('arg1');
+
+ expect(callCount).toBe(2);
+ const key = cachedFn.getKey('arg1');
+ const cached = await redis.get(key);
+ expect(cached).toBeNull();
+ });
+
+ it('should clear a cached empty array entry', async () => {
+ const fn = async (arg1: string) => [] as number[];
+ const cachedFn = cacheable(fn, 3600, { cacheEmptyArray: true });
+
+ await cachedFn('arg1');
+ const key = cachedFn.getKey('arg1');
+ expect(await redis.get(key)).toBe('[]');
+
+ const deleted = await cachedFn.clear('arg1');
+ expect(deleted).toBe(1);
+ expect(await redis.get(key)).toBeNull();
+ });
+
it('should not cache empty objects', async () => {
let fnCalled = false;
const fn = async (arg1: string) => {
@@ -354,14 +455,14 @@ describe('cachable', () => {
const cachedFn = cacheable(
'testFunction',
async (arg1: string) => mockData,
- 3600,
+ 3600
);
const key = cachedFn.getKey('arg1');
await redis.set(key, 'invalid json');
- const consoleSpy = vi
- .spyOn(console, 'error')
- .mockImplementation(() => {});
+ const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {
+ // noop
+ });
let fnCalled = false;
const fn = async (arg1: string) => {
@@ -376,7 +477,7 @@ describe('cachable', () => {
expect(fnCalled).toBe(true);
expect(consoleSpy).toHaveBeenCalledWith(
'Failed to parse cache',
- expect.any(Error),
+ expect.any(Error)
);
consoleSpy.mockRestore();
@@ -390,7 +491,7 @@ describe('cachable', () => {
const cachedFn = cacheable(
'testFunction',
async (arg1: string) => cachedData,
- 3600,
+ 3600
);
await cachedFn('arg1');
@@ -478,7 +579,7 @@ describe('cachable', () => {
arg5: undefined,
arg6: number[],
arg7: { a: number; b: number },
- arg8: Date,
+ arg8: Date
) => ({});
const cachedFn = cacheable(fn, 3600);
@@ -490,7 +591,7 @@ describe('cachable', () => {
undefined,
[1, 2, 3],
{ a: 1, b: 2 },
- new Date('2023-01-01T00:00:00Z'),
+ new Date('2023-01-01T00:00:00Z')
);
expect(key).toMatch(/^cachable:.*:/);
diff --git a/packages/redis/cachable.ts b/packages/redis/cachable.ts
index 03f70d2b6..ae46a69cc 100644
--- a/packages/redis/cachable.ts
+++ b/packages/redis/cachable.ts
@@ -109,7 +109,11 @@ function stringify(obj: unknown): string {
return String(obj);
}
-function hasResult(result: unknown): boolean {
+export interface CacheableOptions {
+ cacheEmptyArray?: boolean;
+}
+
+function shouldCache(result: unknown, options: CacheableOptions = {}): boolean {
// Don't cache undefined or null
if (result === undefined || result === null) {
return false;
@@ -120,9 +124,8 @@ function hasResult(result: unknown): boolean {
return result.length > 0;
}
- // Don't cache empty arrays
if (Array.isArray(result)) {
- return result.length > 0;
+ return options.cacheEmptyArray ? true : result.length > 0;
}
// Don't cache empty objects
@@ -153,10 +156,11 @@ const parseCache = (cached: string) => {
const CACHEABLE_LRU_TTL_MS = 60 * 1000; // 60 seconds
const CACHEABLE_LRU_MAX = 1000;
-// Overload 1: cacheable(fn, expireInSec)
+// Overload 1: cacheable(fn, expireInSec, options?)
export function cacheable<T extends (...args: any) => any>(
fn: T,
- expireInSec: number
+ expireInSec: number,
+ options?: CacheableOptions
): T & {
getKey: (...args: Parameters<T>) => string;
clear: (...args: Parameters<T>) => Promise<number>;
@@ -165,11 +169,12 @@ export function cacheable<T extends (...args: any) => any>(
) => (payload: Awaited<ReturnType<T>>) => Promise<'OK'>;
};
-// Overload 2: cacheable(name, fn, expireInSec)
+// Overload 2: cacheable(name, fn, expireInSec, options?)
export function cacheable<T extends (...args: any) => any>(
name: string,
fn: T,
- expireInSec: number
+ expireInSec: number,
+ options?: CacheableOptions
): T & {
getKey: (...args: Parameters<T>) => string;
clear: (...args: Parameters<T>) => Promise<number>;
@@ -182,7 +187,8 @@ export function cacheable<T extends (...args: any) => any>(
export function cacheable<T extends (...args: any) => any>(
fnOrName: T | string,
fnOrExpireInSec: number | T,
- _expireInSec?: number
+ expireInSecOrOptions?: number | CacheableOptions,
+ maybeOptions?: CacheableOptions
) {
const name = typeof fnOrName === 'string' ? fnOrName : fnOrName.name;
const fn =
@@ -193,14 +199,22 @@ export function cacheable<T extends (...args: any) => any>(
: null;
let expireInSec: number | null = null;
+ let options: CacheableOptions = {};
// Parse parameters based on function signature
if (typeof fnOrName === 'function') {
- // Overload 1: cacheable(fn, expireInSec)
+ // Overload 1: cacheable(fn, expireInSec, options?)
expireInSec = typeof fnOrExpireInSec === 'number' ? fnOrExpireInSec : null;
+ if (expireInSecOrOptions && typeof expireInSecOrOptions === 'object') {
+ options = expireInSecOrOptions;
+ }
} else {
- // Overload 2: cacheable(name, fn, expireInSec)
- expireInSec = typeof _expireInSec === 'number' ? _expireInSec : null;
+ // Overload 2: cacheable(name, fn, expireInSec, options?)
+ expireInSec =
+ typeof expireInSecOrOptions === 'number' ? expireInSecOrOptions : null;
+ if (maybeOptions) {
+ options = maybeOptions;
+ }
}
if (typeof fn !== 'function') {
@@ -228,7 +242,7 @@ export function cacheable<T extends (...args: any) => any>(
// L1: in-memory LRU first (offloads Redis on hot keys)
const lruHit = lruCache.get(key);
- if (lruHit !== undefined && hasResult(lruHit)) {
+ if (lruHit !== undefined && shouldCache(lruHit, options)) {
return lruHit as Awaited<ReturnType<T>>;
}
@@ -236,7 +250,7 @@ export function cacheable<T extends (...args: any) => any>(
const cached = await getRedisCache().get(key);
if (cached) {
const parsed = parseCache(cached);
- if (hasResult(parsed)) {
+ if (shouldCache(parsed, options)) {
lruCache.set(key, parsed);
return parsed;
}
@@ -245,7 +259,7 @@ export function cacheable<T extends (...args: any) => any>(
// Cache miss: execute function
const result = await fn(...(args as any));
- if (hasResult(result)) {
+ if (shouldCache(result, options)) {
lruCache.set(key, result);
getRedisCache()
.setex(key, expireInSec, JSON.stringify(result))
@@ -267,7 +281,7 @@ export function cacheable<T extends (...args: any) => any>(
(...args: Parameters<T>) =>
(payload: Awaited<ReturnType<T>>) => {
const key = getKey(...args);
- if (hasResult(payload)) {
+ if (shouldCache(payload, options)) {
lruCache.set(key, payload);
return getRedisCache()
.setex(key, expireInSec, JSON.stringify(payload))