fix: sync cachable
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { cacheable } from '@openpanel/redis';
|
import { cacheable, cacheableLru } from '@openpanel/redis';
|
||||||
import bots from './bots';
|
import bots from './bots';
|
||||||
|
|
||||||
// Pre-compile regex patterns at module load time
|
// Pre-compile regex patterns at module load time
|
||||||
@@ -15,7 +15,7 @@ const compiledBots = bots.map((bot) => {
|
|||||||
const regexBots = compiledBots.filter((bot) => 'compiledRegex' in bot);
|
const regexBots = compiledBots.filter((bot) => 'compiledRegex' in bot);
|
||||||
const includesBots = compiledBots.filter((bot) => 'includes' in bot);
|
const includesBots = compiledBots.filter((bot) => 'includes' in bot);
|
||||||
|
|
||||||
export const isBot = cacheable(
|
export const isBot = cacheableLru(
|
||||||
'is-bot',
|
'is-bot',
|
||||||
(ua: string) => {
|
(ua: string) => {
|
||||||
// Check simple string patterns first (fast)
|
// Check simple string patterns first (fast)
|
||||||
@@ -40,6 +40,8 @@ export const isBot = cacheable(
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
60 * 60, // 1 hour
|
{
|
||||||
'lru',
|
maxSize: 1000,
|
||||||
|
ttl: 60 * 5,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { cacheable } from '@openpanel/redis';
|
import { cacheable, cacheableLru } from '@openpanel/redis';
|
||||||
import type { Client, Prisma } from '../prisma-client';
|
import type { Client, Prisma } from '../prisma-client';
|
||||||
import { db } from '../prisma-client';
|
import { db } from '../prisma-client';
|
||||||
|
|
||||||
@@ -34,8 +34,7 @@ export async function getClientById(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getClientByIdCached = cacheable(
|
export const getClientByIdCached = cacheableLru(getClientById, {
|
||||||
getClientById,
|
maxSize: 1000,
|
||||||
60 * 60 * 24,
|
ttl: 60 * 5,
|
||||||
'both',
|
});
|
||||||
);
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { generateSalt } from '@openpanel/common/server';
|
import { generateSalt } from '@openpanel/common/server';
|
||||||
|
|
||||||
import { cacheable } from '@openpanel/redis';
|
import { cacheableLru } from '@openpanel/redis';
|
||||||
import { db } from '../prisma-client';
|
import { db } from '../prisma-client';
|
||||||
|
|
||||||
export async function getCurrentSalt() {
|
export async function getCurrentSalt() {
|
||||||
@@ -17,7 +17,7 @@ export async function getCurrentSalt() {
|
|||||||
return salt.salt;
|
return salt.salt;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getSalts = cacheable(
|
export const getSalts = cacheableLru(
|
||||||
'op:salt',
|
'op:salt',
|
||||||
async () => {
|
async () => {
|
||||||
const [curr, prev] = await db.salt.findMany({
|
const [curr, prev] = await db.salt.findMany({
|
||||||
@@ -42,8 +42,10 @@ export const getSalts = cacheable(
|
|||||||
|
|
||||||
return salts;
|
return salts;
|
||||||
},
|
},
|
||||||
60 * 10,
|
{
|
||||||
'both',
|
maxSize: 2,
|
||||||
|
ttl: 60 * 5,
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
export async function createInitialSalts() {
|
export async function createInitialSalts() {
|
||||||
|
|||||||
@@ -128,13 +128,17 @@ function hasResult(result: unknown): boolean {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
type CacheMode = 'lru' | 'redis' | 'both';
|
export interface CacheableLruOptions {
|
||||||
|
/** TTL in seconds for LRU cache */
|
||||||
|
ttl: number;
|
||||||
|
/** Maximum number of entries in LRU cache */
|
||||||
|
maxSize?: number;
|
||||||
|
}
|
||||||
|
|
||||||
// Overload 1: cacheable(fn, expireInSec, lruCache?)
|
// Overload 1: cacheable(fn, expireInSec)
|
||||||
export function cacheable<T extends (...args: any) => any>(
|
export function cacheable<T extends (...args: any) => any>(
|
||||||
fn: T,
|
fn: T,
|
||||||
expireInSec: number,
|
expireInSec: number,
|
||||||
cacheMode?: CacheMode,
|
|
||||||
): T & {
|
): T & {
|
||||||
getKey: (...args: Parameters<T>) => string;
|
getKey: (...args: Parameters<T>) => string;
|
||||||
clear: (...args: Parameters<T>) => Promise<number>;
|
clear: (...args: Parameters<T>) => Promise<number>;
|
||||||
@@ -143,12 +147,11 @@ export function cacheable<T extends (...args: any) => any>(
|
|||||||
) => (payload: Awaited<ReturnType<T>>) => Promise<'OK'>;
|
) => (payload: Awaited<ReturnType<T>>) => Promise<'OK'>;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Overload 2: cacheable(name, fn, expireInSec, lruCache?)
|
// Overload 2: cacheable(name, fn, expireInSec)
|
||||||
export function cacheable<T extends (...args: any) => any>(
|
export function cacheable<T extends (...args: any) => any>(
|
||||||
name: string,
|
name: string,
|
||||||
fn: T,
|
fn: T,
|
||||||
expireInSec: number,
|
expireInSec: number,
|
||||||
cacheMode?: CacheMode,
|
|
||||||
): T & {
|
): T & {
|
||||||
getKey: (...args: Parameters<T>) => string;
|
getKey: (...args: Parameters<T>) => string;
|
||||||
clear: (...args: Parameters<T>) => Promise<number>;
|
clear: (...args: Parameters<T>) => Promise<number>;
|
||||||
@@ -157,12 +160,11 @@ export function cacheable<T extends (...args: any) => any>(
|
|||||||
) => (payload: Awaited<ReturnType<T>>) => Promise<'OK'>;
|
) => (payload: Awaited<ReturnType<T>>) => Promise<'OK'>;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Implementation
|
// Implementation for cacheable (Redis-only - async)
|
||||||
export function cacheable<T extends (...args: any) => any>(
|
export function cacheable<T extends (...args: any) => any>(
|
||||||
fnOrName: T | string,
|
fnOrName: T | string,
|
||||||
fnOrExpireInSec: number | T,
|
fnOrExpireInSec: number | T,
|
||||||
_expireInSecOrCacheMode?: number | CacheMode,
|
_expireInSec?: number,
|
||||||
_cacheMode?: CacheMode,
|
|
||||||
) {
|
) {
|
||||||
const name = typeof fnOrName === 'string' ? fnOrName : fnOrName.name;
|
const name = typeof fnOrName === 'string' ? fnOrName : fnOrName.name;
|
||||||
const fn =
|
const fn =
|
||||||
@@ -173,23 +175,14 @@ export function cacheable<T extends (...args: any) => any>(
|
|||||||
: null;
|
: null;
|
||||||
|
|
||||||
let expireInSec: number | null = null;
|
let expireInSec: number | null = null;
|
||||||
let cacheMode = 'redis';
|
|
||||||
|
|
||||||
// Parse parameters based on function signature
|
// Parse parameters based on function signature
|
||||||
if (typeof fnOrName === 'function') {
|
if (typeof fnOrName === 'function') {
|
||||||
// Overload 1: cacheable(fn, expireInSec, lruCache?)
|
// Overload 1: cacheable(fn, expireInSec)
|
||||||
expireInSec = typeof fnOrExpireInSec === 'number' ? fnOrExpireInSec : null;
|
expireInSec = typeof fnOrExpireInSec === 'number' ? fnOrExpireInSec : null;
|
||||||
cacheMode =
|
|
||||||
typeof _expireInSecOrCacheMode === 'boolean'
|
|
||||||
? _expireInSecOrCacheMode
|
|
||||||
: 'redis';
|
|
||||||
} else {
|
} else {
|
||||||
// Overload 2: cacheable(name, fn, expireInSec, lruCache?)
|
// Overload 2: cacheable(name, fn, expireInSec)
|
||||||
expireInSec =
|
expireInSec = typeof _expireInSec === 'number' ? _expireInSec : null;
|
||||||
typeof _expireInSecOrCacheMode === 'number'
|
|
||||||
? _expireInSecOrCacheMode
|
|
||||||
: null;
|
|
||||||
cacheMode = typeof _cacheMode === 'string' ? _cacheMode : 'redis';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof fn !== 'function') {
|
if (typeof fn !== 'function') {
|
||||||
@@ -204,33 +197,13 @@ export function cacheable<T extends (...args: any) => any>(
|
|||||||
const getKey = (...args: Parameters<T>) =>
|
const getKey = (...args: Parameters<T>) =>
|
||||||
`${cachePrefix}:${stringify(args)}`;
|
`${cachePrefix}:${stringify(args)}`;
|
||||||
|
|
||||||
// Create function-specific LRU cache if enabled
|
// Redis-only mode: asynchronous implementation
|
||||||
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 (
|
const cachedFn = async (
|
||||||
...args: Parameters<T>
|
...args: Parameters<T>
|
||||||
): Promise<Awaited<ReturnType<T>>> => {
|
): Promise<Awaited<ReturnType<T>>> => {
|
||||||
const key = getKey(...args);
|
const key = getKey(...args);
|
||||||
|
|
||||||
// L1 Cache: Check LRU cache first (in-memory, instant)
|
// Check Redis cache (shared across instances)
|
||||||
if (functionLruCache) {
|
|
||||||
const lruHit = functionLruCache.get(key);
|
|
||||||
if (lruHit !== undefined && hasResult(lruHit)) {
|
|
||||||
return lruHit;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cacheMode === 'lru') {
|
|
||||||
return null as any;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// L2 Cache: Check Redis cache (shared across instances)
|
|
||||||
const cached = await getRedisCache().get(key);
|
const cached = await getRedisCache().get(key);
|
||||||
if (cached) {
|
if (cached) {
|
||||||
try {
|
try {
|
||||||
@@ -244,10 +217,6 @@ export function cacheable<T extends (...args: any) => any>(
|
|||||||
return value;
|
return value;
|
||||||
});
|
});
|
||||||
if (hasResult(parsed)) {
|
if (hasResult(parsed)) {
|
||||||
// Store in LRU cache for next time
|
|
||||||
if (functionLruCache) {
|
|
||||||
functionLruCache.set(key, parsed);
|
|
||||||
}
|
|
||||||
return parsed;
|
return parsed;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -259,12 +228,10 @@ export function cacheable<T extends (...args: any) => any>(
|
|||||||
const result = await fn(...(args as any));
|
const result = await fn(...(args as any));
|
||||||
|
|
||||||
if (hasResult(result)) {
|
if (hasResult(result)) {
|
||||||
// Store in both caches
|
|
||||||
if (functionLruCache) {
|
|
||||||
functionLruCache.set(key, result);
|
|
||||||
}
|
|
||||||
// Don't await Redis write - fire and forget for better performance
|
// Don't await Redis write - fire and forget for better performance
|
||||||
getRedisCache().setex(key, expireInSec, JSON.stringify(result));
|
getRedisCache()
|
||||||
|
.setex(key, expireInSec, JSON.stringify(result))
|
||||||
|
.catch(() => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
@@ -273,21 +240,140 @@ export function cacheable<T extends (...args: any) => any>(
|
|||||||
cachedFn.getKey = getKey;
|
cachedFn.getKey = getKey;
|
||||||
cachedFn.clear = async (...args: Parameters<T>) => {
|
cachedFn.clear = async (...args: Parameters<T>) => {
|
||||||
const key = getKey(...args);
|
const key = getKey(...args);
|
||||||
// Clear both LRU and Redis caches
|
|
||||||
if (functionLruCache) {
|
|
||||||
functionLruCache.delete(key);
|
|
||||||
}
|
|
||||||
return getRedisCache().del(key);
|
return getRedisCache().del(key);
|
||||||
};
|
};
|
||||||
cachedFn.set =
|
cachedFn.set =
|
||||||
(...args: Parameters<T>) =>
|
(...args: Parameters<T>) =>
|
||||||
async (payload: Awaited<ReturnType<T>>) => {
|
async (payload: Awaited<ReturnType<T>>) => {
|
||||||
const key = getKey(...args);
|
const key = getKey(...args);
|
||||||
// Set in both caches
|
return getRedisCache()
|
||||||
if (functionLruCache) {
|
.setex(key, expireInSec, JSON.stringify(payload))
|
||||||
functionLruCache.set(key, payload);
|
.catch(() => {});
|
||||||
}
|
};
|
||||||
return getRedisCache().setex(key, expireInSec, JSON.stringify(payload));
|
|
||||||
|
return cachedFn;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Overload 1: cacheableLru(fn, options)
|
||||||
|
export function cacheableLru<T extends (...args: any) => any>(
|
||||||
|
fn: T,
|
||||||
|
options: CacheableLruOptions,
|
||||||
|
): T & {
|
||||||
|
getKey: (...args: Parameters<T>) => string;
|
||||||
|
clear: (...args: Parameters<T>) => boolean;
|
||||||
|
set: (...args: Parameters<T>) => (payload: ReturnType<T>) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Overload 2: cacheableLru(name, fn, options)
|
||||||
|
export function cacheableLru<T extends (...args: any) => any>(
|
||||||
|
name: string,
|
||||||
|
fn: T,
|
||||||
|
options: CacheableLruOptions,
|
||||||
|
): T & {
|
||||||
|
getKey: (...args: Parameters<T>) => string;
|
||||||
|
clear: (...args: Parameters<T>) => boolean;
|
||||||
|
set: (...args: Parameters<T>) => (payload: ReturnType<T>) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Implementation for cacheableLru (LRU-only - synchronous)
|
||||||
|
export function cacheableLru<T extends (...args: any) => any>(
|
||||||
|
fnOrName: T | string,
|
||||||
|
fnOrOptions: T | CacheableLruOptions,
|
||||||
|
_options?: CacheableLruOptions,
|
||||||
|
) {
|
||||||
|
const name = typeof fnOrName === 'string' ? fnOrName : fnOrName.name;
|
||||||
|
const fn =
|
||||||
|
typeof fnOrName === 'function'
|
||||||
|
? fnOrName
|
||||||
|
: typeof fnOrOptions === 'function'
|
||||||
|
? fnOrOptions
|
||||||
|
: null;
|
||||||
|
|
||||||
|
let options: CacheableLruOptions;
|
||||||
|
|
||||||
|
// Parse parameters based on function signature
|
||||||
|
if (typeof fnOrName === 'function') {
|
||||||
|
// Overload 1: cacheableLru(fn, options)
|
||||||
|
options =
|
||||||
|
typeof fnOrOptions === 'object' && fnOrOptions !== null
|
||||||
|
? fnOrOptions
|
||||||
|
: ({} as CacheableLruOptions);
|
||||||
|
} else {
|
||||||
|
// Overload 2: cacheableLru(name, fn, options)
|
||||||
|
options =
|
||||||
|
typeof _options === 'object' && _options !== null
|
||||||
|
? _options
|
||||||
|
: ({} as CacheableLruOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof fn !== 'function') {
|
||||||
|
throw new Error('fn is not a function');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof options.ttl !== 'number') {
|
||||||
|
throw new Error('options.ttl is required and must be a number');
|
||||||
|
}
|
||||||
|
|
||||||
|
const cachePrefix = `cachable:${name}`;
|
||||||
|
const getKey = (...args: Parameters<T>) =>
|
||||||
|
`${cachePrefix}:${stringify(args)}`;
|
||||||
|
|
||||||
|
const maxSize = options.maxSize ?? 1000;
|
||||||
|
const ttl = options.ttl;
|
||||||
|
|
||||||
|
// Create function-specific LRU cache
|
||||||
|
const functionLruCache = new LRUCache<string, any>({
|
||||||
|
max: maxSize,
|
||||||
|
ttl: ttl * 1000, // Convert seconds to milliseconds for LRU
|
||||||
|
});
|
||||||
|
|
||||||
|
// LRU-only mode: synchronous implementation (or returns promise if fn is async)
|
||||||
|
const cachedFn = ((...args: Parameters<T>): ReturnType<T> => {
|
||||||
|
const key = getKey(...args);
|
||||||
|
|
||||||
|
// Check LRU cache
|
||||||
|
const lruHit = functionLruCache.get(key);
|
||||||
|
if (lruHit !== undefined && hasResult(lruHit)) {
|
||||||
|
return lruHit as ReturnType<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache miss: Execute function
|
||||||
|
const result = fn(...(args as any)) as ReturnType<T>;
|
||||||
|
|
||||||
|
// If result is a Promise, handle it asynchronously but cache the resolved value
|
||||||
|
if (result && typeof (result as any).then === 'function') {
|
||||||
|
return (result as Promise<any>).then((resolved: any) => {
|
||||||
|
if (hasResult(resolved)) {
|
||||||
|
functionLruCache.set(key, resolved);
|
||||||
|
}
|
||||||
|
return resolved;
|
||||||
|
}) as ReturnType<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Synchronous result: cache and return
|
||||||
|
if (hasResult(result)) {
|
||||||
|
functionLruCache.set(key, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}) as T & {
|
||||||
|
getKey: (...args: Parameters<T>) => string;
|
||||||
|
clear: (...args: Parameters<T>) => boolean;
|
||||||
|
set: (...args: Parameters<T>) => (payload: ReturnType<T>) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
cachedFn.getKey = getKey;
|
||||||
|
cachedFn.clear = (...args: Parameters<T>) => {
|
||||||
|
const key = getKey(...args);
|
||||||
|
return functionLruCache.delete(key);
|
||||||
|
};
|
||||||
|
cachedFn.set =
|
||||||
|
(...args: Parameters<T>) =>
|
||||||
|
(payload: ReturnType<T>) => {
|
||||||
|
const key = getKey(...args);
|
||||||
|
if (hasResult(payload)) {
|
||||||
|
functionLruCache.set(key, payload);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return cachedFn;
|
return cachedFn;
|
||||||
|
|||||||
Reference in New Issue
Block a user