From 101ea217476606c1619fc8226c6cf29fe0d069d3 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sat, 4 Nov 2023 11:54:13 +0100 Subject: [PATCH] server: don't cache users for infinity If the cache runs for an infinite amount of time, the users may as well be stored in memory directly. Changelog: Fixed --- packages/backend/src/misc/keypair-store.ts | 3 ++- packages/backend/src/services/user-cache.ts | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/backend/src/misc/keypair-store.ts b/packages/backend/src/misc/keypair-store.ts index 910f96258..e363d93ac 100644 --- a/packages/backend/src/misc/keypair-store.ts +++ b/packages/backend/src/misc/keypair-store.ts @@ -2,9 +2,10 @@ import { UserKeypairs } from '@/models/index.js'; import { User } from '@/models/entities/user.js'; import { UserKeypair } from '@/models/entities/user-keypair.js'; import { Cache } from './cache.js'; +import { MINUTE } from '@/const.js'; const cache = new Cache( - Infinity, + 15 * MINUTE, (userId) => UserKeypairs.findOneByOrFail({ userId }), ); diff --git a/packages/backend/src/services/user-cache.ts b/packages/backend/src/services/user-cache.ts index 9638f8101..36ce444c8 100644 --- a/packages/backend/src/services/user-cache.ts +++ b/packages/backend/src/services/user-cache.ts @@ -3,17 +3,18 @@ import { ILocalUser, User } from '@/models/entities/user.js'; import { Users } from '@/models/index.js'; import { Cache } from '@/misc/cache.js'; import { subscriber } from '@/db/redis.js'; +import { MINUTE } from '@/const.js'; export const userByIdCache = new Cache( - Infinity, + 15 * MINUTE, async (id) => await Users.findOneBy({ id, isDeleted: IsNull() }) ?? undefined, ); export const localUserByNativeTokenCache = new Cache( - Infinity, + 15 * MINUTE, async (token) => await Users.findOneBy({ token, host: IsNull(), isDeleted: IsNull() }) as ILocalUser | null ?? undefined, ); export const uriPersonCache = new Cache( - Infinity, + 15 * MINUTE, async (uri) => await Users.findOneBy({ uri, isDeleted: IsNull() }) ?? undefined, );