Skip to content

Commit 336b246

Browse files
committed
fix: add user-cache (TTL)
1 parent 2004c6f commit 336b246

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/const.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export const GET_HTML_SKELETON_CACHE_TTL_SECS = 5 * 60; // 5 minutes
7676
export const GET_HTML_SKELETON_CACHE_MAX_SIZE = 200;
7777
export const MCP_SERVER_CACHE_MAX_SIZE = 500;
7878
export const MCP_SERVER_CACHE_TTL_SECS = 30 * 60; // 30 minutes
79+
export const USER_CACHE_MAX_SIZE = 200;
80+
export const USER_CACHE_TTL_SECS = 60 * 60; // 1 hour
7981

8082
export const ACTOR_PRICING_MODEL = {
8183
/** Rental Actors */

src/utils/user-cache.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { createHash } from 'node:crypto';
33
import type { User } from 'apify-client';
44

55
import type { ApifyClient } from '../apify-client.js';
6+
import { USER_CACHE_MAX_SIZE, USER_CACHE_TTL_SECS } from '../const.js';
7+
import { TTLLRUCache } from './ttl-lru.js';
68

7-
// Type for cached user info - stores the raw User object from API
8-
const userCache = new Map<string, User>();
9+
// LRU cache with TTL for user info - stores the raw User object from API
10+
const userCache = new TTLLRUCache<User>(USER_CACHE_MAX_SIZE, USER_CACHE_TTL_SECS);
911

1012
/**
1113
* Gets user info from token, using cache to avoid repeated API calls
@@ -20,8 +22,9 @@ export async function getUserIdFromToken(
2022
const tokenHash = createHash('sha256').update(token).digest('hex');
2123

2224
// Check cache first
23-
if (userCache.has(tokenHash)) {
24-
return userCache.get(tokenHash)!;
25+
const cachedUser = userCache.get(tokenHash);
26+
if (cachedUser) {
27+
return cachedUser;
2528
}
2629

2730
// Fetch from API

0 commit comments

Comments
 (0)