wip
This commit is contained in:
@@ -34,4 +34,8 @@ export async function getClientById(
|
||||
});
|
||||
}
|
||||
|
||||
export const getClientByIdCached = cacheable(getClientById, 60 * 60 * 24, true);
|
||||
export const getClientByIdCached = cacheable(
|
||||
getClientById,
|
||||
60 * 60 * 24,
|
||||
'both',
|
||||
);
|
||||
|
||||
@@ -340,7 +340,7 @@ export async function createEvent(payload: IServiceCreateEventPayload) {
|
||||
sdk_version: payload.sdkVersion ?? '',
|
||||
};
|
||||
|
||||
await Promise.all([sessionBuffer.add(event), eventBuffer.add(event)]);
|
||||
const promises = [sessionBuffer.add(event), eventBuffer.add(event)];
|
||||
|
||||
if (payload.profileId) {
|
||||
const profile: IServiceUpsertProfile = {
|
||||
@@ -371,10 +371,12 @@ export async function createEvent(payload: IServiceCreateEventPayload) {
|
||||
profile.isExternal ||
|
||||
(profile.isExternal === false && payload.name === 'session_start')
|
||||
) {
|
||||
await upsertProfile(profile, true);
|
||||
promises.push(upsertProfile(profile, true));
|
||||
}
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
|
||||
return {
|
||||
document: event,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { generateSalt } from '@openpanel/common/server';
|
||||
|
||||
import { cacheable, getRedisCache } from '@openpanel/redis';
|
||||
import { cacheable } from '@openpanel/redis';
|
||||
import { db } from '../prisma-client';
|
||||
|
||||
export async function getCurrentSalt() {
|
||||
@@ -43,7 +43,7 @@ export const getSalts = cacheable(
|
||||
return salts;
|
||||
},
|
||||
60 * 10,
|
||||
true,
|
||||
'both',
|
||||
);
|
||||
|
||||
export async function createInitialSalts() {
|
||||
|
||||
@@ -128,11 +128,13 @@ function hasResult(result: unknown): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
type CacheMode = 'lru' | 'redis' | 'both';
|
||||
|
||||
// Overload 1: cacheable(fn, expireInSec, lruCache?)
|
||||
export function cacheable<T extends (...args: any) => any>(
|
||||
fn: T,
|
||||
expireInSec: number,
|
||||
lruCache?: boolean,
|
||||
cacheMode?: CacheMode,
|
||||
): T & {
|
||||
getKey: (...args: Parameters<T>) => string;
|
||||
clear: (...args: Parameters<T>) => Promise<number>;
|
||||
@@ -146,7 +148,7 @@ export function cacheable<T extends (...args: any) => any>(
|
||||
name: string,
|
||||
fn: T,
|
||||
expireInSec: number,
|
||||
lruCache?: boolean,
|
||||
cacheMode?: CacheMode,
|
||||
): T & {
|
||||
getKey: (...args: Parameters<T>) => string;
|
||||
clear: (...args: Parameters<T>) => Promise<number>;
|
||||
@@ -159,8 +161,8 @@ export function cacheable<T extends (...args: any) => any>(
|
||||
export function cacheable<T extends (...args: any) => any>(
|
||||
fnOrName: T | string,
|
||||
fnOrExpireInSec: number | T,
|
||||
_expireInSecOrLruCache?: number | boolean,
|
||||
_lruCache?: boolean,
|
||||
_expireInSecOrCacheMode?: number | CacheMode,
|
||||
_cacheMode?: CacheMode,
|
||||
) {
|
||||
const name = typeof fnOrName === 'string' ? fnOrName : fnOrName.name;
|
||||
const fn =
|
||||
@@ -171,23 +173,23 @@ export function cacheable<T extends (...args: any) => any>(
|
||||
: null;
|
||||
|
||||
let expireInSec: number | null = null;
|
||||
let useLruCache = false;
|
||||
let cacheMode = 'redis';
|
||||
|
||||
// Parse parameters based on function signature
|
||||
if (typeof fnOrName === 'function') {
|
||||
// Overload 1: cacheable(fn, expireInSec, lruCache?)
|
||||
expireInSec = typeof fnOrExpireInSec === 'number' ? fnOrExpireInSec : null;
|
||||
useLruCache =
|
||||
typeof _expireInSecOrLruCache === 'boolean'
|
||||
? _expireInSecOrLruCache
|
||||
: false;
|
||||
cacheMode =
|
||||
typeof _expireInSecOrCacheMode === 'boolean'
|
||||
? _expireInSecOrCacheMode
|
||||
: 'redis';
|
||||
} else {
|
||||
// Overload 2: cacheable(name, fn, expireInSec, lruCache?)
|
||||
expireInSec =
|
||||
typeof _expireInSecOrLruCache === 'number'
|
||||
? _expireInSecOrLruCache
|
||||
typeof _expireInSecOrCacheMode === 'number'
|
||||
? _expireInSecOrCacheMode
|
||||
: null;
|
||||
useLruCache = typeof _lruCache === 'boolean' ? _lruCache : false;
|
||||
cacheMode = typeof _cacheMode === 'string' ? _cacheMode : 'redis';
|
||||
}
|
||||
|
||||
if (typeof fn !== 'function') {
|
||||
@@ -203,12 +205,13 @@ export function cacheable<T extends (...args: any) => any>(
|
||||
`${cachePrefix}:${stringify(args)}`;
|
||||
|
||||
// Create function-specific LRU cache if enabled
|
||||
const functionLruCache = useLruCache
|
||||
? new LRUCache<string, any>({
|
||||
max: 1000,
|
||||
ttl: expireInSec * 1000, // Convert seconds to milliseconds for LRU
|
||||
})
|
||||
: null;
|
||||
const functionLruCache =
|
||||
cacheMode === 'lru' || cacheMode === 'both'
|
||||
? new LRUCache<string, any>({
|
||||
max: 1000,
|
||||
ttl: expireInSec * 1000, // Convert seconds to milliseconds for LRU
|
||||
})
|
||||
: null;
|
||||
|
||||
const cachedFn = async (
|
||||
...args: Parameters<T>
|
||||
@@ -221,6 +224,10 @@ export function cacheable<T extends (...args: any) => any>(
|
||||
if (lruHit !== undefined && hasResult(lruHit)) {
|
||||
return lruHit;
|
||||
}
|
||||
|
||||
if (cacheMode === 'lru') {
|
||||
return null as any;
|
||||
}
|
||||
}
|
||||
|
||||
// L2 Cache: Check Redis cache (shared across instances)
|
||||
|
||||
Reference in New Issue
Block a user