From 76c9422d538bd28081bf952741b4b7c629afb809 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Sun, 2 Jul 2023 20:55:20 -0400 Subject: [PATCH] add cache prefix --- packages/backend/src/misc/cache.ts | 11 +++++------ packages/backend/src/misc/check-hit-antenna.ts | 2 +- packages/backend/src/misc/emoji-meta.ts | 2 +- packages/backend/src/misc/keypair-store.ts | 2 +- packages/backend/src/misc/populate-emojis.ts | 2 +- packages/backend/src/models/repositories/user.ts | 2 +- .../backend/src/remote/activitypub/db-resolver.ts | 4 ++-- packages/backend/src/server/api/authenticate.ts | 2 +- packages/backend/src/server/nodeinfo.ts | 2 +- packages/backend/src/services/instance-actor.ts | 2 +- packages/backend/src/services/note/create.ts | 2 +- .../src/services/register-or-fetch-instance-doc.ts | 2 +- packages/backend/src/services/relay.ts | 2 +- packages/backend/src/services/user-cache.ts | 8 ++++---- 14 files changed, 22 insertions(+), 23 deletions(-) diff --git a/packages/backend/src/misc/cache.ts b/packages/backend/src/misc/cache.ts index d790313d18..c2695aa789 100644 --- a/packages/backend/src/misc/cache.ts +++ b/packages/backend/src/misc/cache.ts @@ -1,19 +1,18 @@ import { redisClient } from "@/db/redis.js"; -import { nativeRandomStr } from "native-utils/built/index.js"; import { encode, decode } from "@msgpack/msgpack"; import { ChainableCommander } from "ioredis"; export class Cache { private ttl: number; - private fingerprint: string; + private prefix: string; - constructor(ttl: number) { + constructor(prefix: string, ttl: number) { this.ttl = ttl; - this.fingerprint = `cache:${nativeRandomStr(32)}`; + this.prefix = `cache:${prefix}`; } private prefixedKey(key: string | null): string { - return key ? `${this.fingerprint}:${key}` : this.fingerprint; + return key ? `${this.prefix}:${key}` : this.prefix; } public async set(key: string | null, value: T, transaction?: ChainableCommander): Promise { @@ -36,7 +35,7 @@ export class Cache { } public async getAll(): Promise> { - const keys = await redisClient.keys(`${this.fingerprint}*`); + const keys = await redisClient.keys(`${this.prefix}*`); const map = new Map(); if (keys.length === 0) { return map; diff --git a/packages/backend/src/misc/check-hit-antenna.ts b/packages/backend/src/misc/check-hit-antenna.ts index 358fba0f37..c422cca943 100644 --- a/packages/backend/src/misc/check-hit-antenna.ts +++ b/packages/backend/src/misc/check-hit-antenna.ts @@ -11,7 +11,7 @@ import * as Acct from "@/misc/acct.js"; import type { Packed } from "./schema.js"; import { Cache } from "./cache.js"; -const blockingCache = new Cache(1000 * 60 * 5); +const blockingCache = new Cache("blocking", 1000 * 60 * 5); // NOTE: フォローしているユーザーのノート、リストのユーザーのノート、グループのユーザーのノート指定はパフォーマンス上の理由で無効になっている diff --git a/packages/backend/src/misc/emoji-meta.ts b/packages/backend/src/misc/emoji-meta.ts index 45364bdcbc..d2d15411f7 100644 --- a/packages/backend/src/misc/emoji-meta.ts +++ b/packages/backend/src/misc/emoji-meta.ts @@ -11,7 +11,7 @@ export type Size = { height: number; }; -const cache = new Cache(1000 * 60 * 10); // once every 10 minutes for the same url +const cache = new Cache("emojiMeta",1000 * 60 * 10); // once every 10 minutes for the same url const logger = new Logger("emoji"); export async function getEmojiSize(url: string): Promise { diff --git a/packages/backend/src/misc/keypair-store.ts b/packages/backend/src/misc/keypair-store.ts index 4551bfd988..b0e07c4ab4 100644 --- a/packages/backend/src/misc/keypair-store.ts +++ b/packages/backend/src/misc/keypair-store.ts @@ -3,7 +3,7 @@ import type { User } from "@/models/entities/user.js"; import type { UserKeypair } from "@/models/entities/user-keypair.js"; import { Cache } from "./cache.js"; -const cache = new Cache(Infinity); +const cache = new Cache("keypairStore", Infinity); export async function getUserKeypair(userId: User["id"]): Promise { return await cache.fetch(userId, () => diff --git a/packages/backend/src/misc/populate-emojis.ts b/packages/backend/src/misc/populate-emojis.ts index ce25dd5594..e6e6c2fb90 100644 --- a/packages/backend/src/misc/populate-emojis.ts +++ b/packages/backend/src/misc/populate-emojis.ts @@ -9,7 +9,7 @@ import config from "@/config/index.js"; import { query } from "@/prelude/url.js"; import { redisClient } from "@/db/redis.js"; -const cache = new Cache(1000 * 60 * 60 * 12); +const cache = new Cache("populateEmojis", 1000 * 60 * 60 * 12); /** * 添付用絵文字情報 diff --git a/packages/backend/src/models/repositories/user.ts b/packages/backend/src/models/repositories/user.ts index 48c8d75b3b..2bd8d4fbaa 100644 --- a/packages/backend/src/models/repositories/user.ts +++ b/packages/backend/src/models/repositories/user.ts @@ -40,7 +40,7 @@ import { } from "../index.js"; import type { Instance } from "../entities/instance.js"; -const userInstanceCache = new Cache(1000 * 60 * 60 * 3); +const userInstanceCache = new Cache("userInstance", 1000 * 60 * 60 * 3); type IsUserDetailed = Detailed extends true ? Packed<"UserDetailed"> diff --git a/packages/backend/src/remote/activitypub/db-resolver.ts b/packages/backend/src/remote/activitypub/db-resolver.ts index 6e448d4b17..4b4ea96270 100644 --- a/packages/backend/src/remote/activitypub/db-resolver.ts +++ b/packages/backend/src/remote/activitypub/db-resolver.ts @@ -20,8 +20,8 @@ import type { IObject } from "./type.js"; import { getApId } from "./type.js"; import { resolvePerson } from "./models/person.js"; -const publicKeyCache = new Cache(Infinity); -const publicKeyByUserIdCache = new Cache(Infinity); +const publicKeyCache = new Cache("publicKey", Infinity); +const publicKeyByUserIdCache = new Cache("publicKeyByUserId", Infinity); export type UriParseResult = | { diff --git a/packages/backend/src/server/api/authenticate.ts b/packages/backend/src/server/api/authenticate.ts index 42274ad2a4..b6fa973eb2 100644 --- a/packages/backend/src/server/api/authenticate.ts +++ b/packages/backend/src/server/api/authenticate.ts @@ -9,7 +9,7 @@ import { localUserByNativeTokenCache, } from "@/services/user-cache.js"; -const appCache = new Cache(Infinity); +const appCache = new Cache("app", Infinity); export class AuthenticationError extends Error { constructor(message: string) { diff --git a/packages/backend/src/server/nodeinfo.ts b/packages/backend/src/server/nodeinfo.ts index dbfb28ff6a..28cefd2cf7 100644 --- a/packages/backend/src/server/nodeinfo.ts +++ b/packages/backend/src/server/nodeinfo.ts @@ -100,7 +100,7 @@ const nodeinfo2 = async () => { }; }; -const cache = new Cache>>(1000 * 60 * 10); +const cache = new Cache>>("nodeinfo", 1000 * 60 * 10); router.get(nodeinfo2_1path, async (ctx) => { const base = await cache.fetch(null, () => nodeinfo2()); diff --git a/packages/backend/src/services/instance-actor.ts b/packages/backend/src/services/instance-actor.ts index 9240f31073..1822cb3c7c 100644 --- a/packages/backend/src/services/instance-actor.ts +++ b/packages/backend/src/services/instance-actor.ts @@ -6,7 +6,7 @@ import { IsNull } from "typeorm"; const ACTOR_USERNAME = "instance.actor" as const; -const cache = new Cache(Infinity); +const cache = new Cache("instanceActor", Infinity); export async function getInstanceActor(): Promise { const cached = await cache.get(null); diff --git a/packages/backend/src/services/note/create.ts b/packages/backend/src/services/note/create.ts index f00678ce22..ca0f05f2c3 100644 --- a/packages/backend/src/services/note/create.ts +++ b/packages/backend/src/services/note/create.ts @@ -73,7 +73,7 @@ import { Mutex } from "redis-semaphore"; const mutedWordsCache = new Cache< { userId: UserProfile["userId"]; mutedWords: UserProfile["mutedWords"] }[] ->(1000 * 60 * 5); +>("mutedWords", 1000 * 60 * 5); type NotificationType = "reply" | "renote" | "quote" | "mention"; diff --git a/packages/backend/src/services/register-or-fetch-instance-doc.ts b/packages/backend/src/services/register-or-fetch-instance-doc.ts index ddb9ce2413..e0c2880376 100644 --- a/packages/backend/src/services/register-or-fetch-instance-doc.ts +++ b/packages/backend/src/services/register-or-fetch-instance-doc.ts @@ -4,7 +4,7 @@ import { genId } from "@/misc/gen-id.js"; import { toPuny } from "@/misc/convert-host.js"; import { Cache } from "@/misc/cache.js"; -const cache = new Cache(1000 * 60 * 60); +const cache = new Cache("registerOrFetchInstanceDoc", 1000 * 60 * 60); export async function registerOrFetchInstanceDoc( host: string, diff --git a/packages/backend/src/services/relay.ts b/packages/backend/src/services/relay.ts index 2325f76c69..1ec2891e8a 100644 --- a/packages/backend/src/services/relay.ts +++ b/packages/backend/src/services/relay.ts @@ -15,7 +15,7 @@ import { createSystemUser } from "./create-system-user.js"; const ACTOR_USERNAME = "relay.actor" as const; -const relaysCache = new Cache(1000 * 60 * 10); +const relaysCache = new Cache("relay", 1000 * 60 * 10); export async function getRelayActor(): Promise { const user = await Users.findOneBy({ diff --git a/packages/backend/src/services/user-cache.ts b/packages/backend/src/services/user-cache.ts index 373fb86869..7fde21d873 100644 --- a/packages/backend/src/services/user-cache.ts +++ b/packages/backend/src/services/user-cache.ts @@ -3,17 +3,17 @@ import type { CacheableUser, ILocalUser, } from "@/models/entities/user.js"; -import { User } from "@/models/entities/user.js"; import { Users } from "@/models/index.js"; import { Cache } from "@/misc/cache.js"; import { redisClient, subscriber } from "@/db/redis.js"; -export const userByIdCache = new Cache(Infinity); +export const userByIdCache = new Cache("userById", Infinity); export const localUserByNativeTokenCache = new Cache( + "localUserByNativeToken", Infinity, ); -export const localUserByIdCache = new Cache(Infinity); -export const uriPersonCache = new Cache(Infinity); +export const localUserByIdCache = new Cache("localUserByIdCache", Infinity); +export const uriPersonCache = new Cache("uriPerson", Infinity); subscriber.on("message", async (_, data) => { const obj = JSON.parse(data);