From ef724a49fe52050aed5b12e201f33c901f25f6f8 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Sun, 2 Jul 2023 20:37:46 -0400 Subject: [PATCH 1/5] store cache values to redis --- packages/backend/package.json | 2 +- packages/backend/src/misc/cache.ts | 86 +++++++++++++------ packages/backend/src/misc/emoji-meta.ts | 34 +++++--- packages/backend/src/misc/populate-emojis.ts | 9 +- .../src/remote/activitypub/models/person.ts | 6 +- .../src/services/chart/charts/active-users.ts | 6 +- .../backend/src/services/instance-actor.ts | 6 +- .../register-or-fetch-instance-doc.ts | 12 +-- packages/backend/src/services/relay.ts | 2 +- packages/backend/src/services/user-cache.ts | 33 +++---- pnpm-lock.yaml | 23 +++-- 11 files changed, 131 insertions(+), 88 deletions(-) diff --git a/packages/backend/package.json b/packages/backend/package.json index b584a5691..6f6344102 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -34,6 +34,7 @@ "@koa/cors": "3.4.3", "@koa/multer": "3.0.2", "@koa/router": "9.0.1", + "@msgpack/msgpack": "3.0.0-beta2", "@peertube/http-signature": "1.7.0", "@redocly/openapi-core": "1.0.0-beta.120", "@sinonjs/fake-timers": "9.1.2", @@ -43,7 +44,6 @@ "ajv": "8.12.0", "archiver": "5.3.1", "argon2": "^0.30.3", - "async-mutex": "^0.4.0", "autobind-decorator": "2.4.0", "autolinker": "4.0.0", "autwh": "0.1.0", diff --git a/packages/backend/src/misc/cache.ts b/packages/backend/src/misc/cache.ts index 9abebc91c..d790313d1 100644 --- a/packages/backend/src/misc/cache.ts +++ b/packages/backend/src/misc/cache.ts @@ -1,43 +1,75 @@ +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 { - public cache: Map; - private lifetime: number; + private ttl: number; + private fingerprint: string; - constructor(lifetime: Cache["lifetime"]) { - this.cache = new Map(); - this.lifetime = lifetime; + constructor(ttl: number) { + this.ttl = ttl; + this.fingerprint = `cache:${nativeRandomStr(32)}`; } - public set(key: string | null, value: T): void { - this.cache.set(key, { - date: Date.now(), - value, - }); + private prefixedKey(key: string | null): string { + return key ? `${this.fingerprint}:${key}` : this.fingerprint; } - public get(key: string | null): T | undefined { - const cached = this.cache.get(key); - if (cached == null) return undefined; - if (Date.now() - cached.date > this.lifetime) { - this.cache.delete(key); - return undefined; + public async set(key: string | null, value: T, transaction?: ChainableCommander): Promise { + const _key = this.prefixedKey(key); + const _value = Buffer.from(encode(value)); + const commander = transaction ?? redisClient; + if (this.ttl === Infinity) { + await commander.set(_key, _value); + } else { + await commander.set(_key, _value, "PX", this.ttl); } - return cached.value; } - public delete(key: string | null) { - this.cache.delete(key); + public async get(key: string | null): Promise { + const _key = this.prefixedKey(key); + const cached = await redisClient.getBuffer(_key); + if (cached === null) return undefined; + + return decode(cached) as T; + } + + public async getAll(): Promise> { + const keys = await redisClient.keys(`${this.fingerprint}*`); + const map = new Map(); + if (keys.length === 0) { + return map; + } + const values = await redisClient.mgetBuffer(keys); + + for (const [i, key] of keys.entries()) { + const val = values[i]; + if (val !== null) { + map.set(key, decode(val) as T); + } + } + + return map; + } + + public async delete(...keys: (string | null)[]): Promise { + if (keys.length > 0) { + const _keys = keys.map(this.prefixedKey); + await redisClient.del(_keys); + } } /** - * キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します - * optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします + * Returns if cached value exists. Otherwise, calls fetcher and caches. + * Overwrites cached value if invalidated by the optional validator. */ public async fetch( key: string | null, fetcher: () => Promise, validator?: (cachedValue: T) => boolean, ): Promise { - const cachedValue = this.get(key); + const cachedValue = await this.get(key); if (cachedValue !== undefined) { if (validator) { if (validator(cachedValue)) { @@ -52,20 +84,20 @@ export class Cache { // Cache MISS const value = await fetcher(); - this.set(key, value); + await this.set(key, value); return value; } /** - * キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します - * optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします + * Returns if cached value exists. Otherwise, calls fetcher and caches if the fetcher returns a value. + * Overwrites cached value if invalidated by the optional validator. */ public async fetchMaybe( key: string | null, fetcher: () => Promise, validator?: (cachedValue: T) => boolean, ): Promise { - const cachedValue = this.get(key); + const cachedValue = await this.get(key); if (cachedValue !== undefined) { if (validator) { if (validator(cachedValue)) { @@ -81,7 +113,7 @@ export class Cache { // Cache MISS const value = await fetcher(); if (value !== undefined) { - this.set(key, value); + await this.set(key, value); } return value; } diff --git a/packages/backend/src/misc/emoji-meta.ts b/packages/backend/src/misc/emoji-meta.ts index fd9d9baa5..45364bdcb 100644 --- a/packages/backend/src/misc/emoji-meta.ts +++ b/packages/backend/src/misc/emoji-meta.ts @@ -1,9 +1,10 @@ import probeImageSize from "probe-image-size"; -import { Mutex, withTimeout } from "async-mutex"; +import { Mutex } from "redis-semaphore"; import { FILE_TYPE_BROWSERSAFE } from "@/const.js"; import Logger from "@/services/logger.js"; import { Cache } from "./cache.js"; +import { redisClient } from "@/db/redis.js"; export type Size = { width: number; @@ -11,23 +12,30 @@ export type Size = { }; const cache = new Cache(1000 * 60 * 10); // once every 10 minutes for the same url -const mutex = withTimeout(new Mutex(), 1000); +const logger = new Logger("emoji"); export async function getEmojiSize(url: string): Promise { - const logger = new Logger("emoji"); + let attempted = true; - await mutex.runExclusive(() => { - const attempted = cache.get(url); - if (!attempted) { - cache.set(url, true); - } else { - logger.warn(`Attempt limit exceeded: ${url}`); - throw new Error("Too many attempts"); - } - }); + const lock = new Mutex(redisClient, "getEmojiSize"); + await lock.acquire(); try { - logger.info(`Retrieving emoji size from ${url}`); + attempted = (await cache.get(url)) === true; + if (!attempted) { + await cache.set(url, true); + } + } finally { + await lock.release(); + } + + if (attempted) { + logger.warn(`Attempt limit exceeded: ${url}`); + throw new Error("Too many attempts"); + } + + try { + logger.debug(`Retrieving emoji size from ${url}`); const { width, height, mime } = await probeImageSize(url, { timeout: 5000, }); diff --git a/packages/backend/src/misc/populate-emojis.ts b/packages/backend/src/misc/populate-emojis.ts index 7aee4ec25..ce25dd559 100644 --- a/packages/backend/src/misc/populate-emojis.ts +++ b/packages/backend/src/misc/populate-emojis.ts @@ -7,6 +7,7 @@ import { isSelfHost, toPunyNullable } from "./convert-host.js"; import { decodeReaction } from "./reaction-lib.js"; 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); @@ -75,7 +76,7 @@ export async function populateEmoji( if (emoji && !(emoji.width && emoji.height)) { emoji = await queryOrNull(); - cache.set(cacheKey, emoji); + await cache.set(cacheKey, emoji); } if (emoji == null) return null; @@ -150,7 +151,7 @@ export async function prefetchEmojis( emojis: { name: string; host: string | null }[], ): Promise { const notCachedEmojis = emojis.filter( - (emoji) => cache.get(`${emoji.name} ${emoji.host}`) == null, + async (emoji) => !(await cache.get(`${emoji.name} ${emoji.host}`)), ); const emojisQuery: any[] = []; const hosts = new Set(notCachedEmojis.map((e) => e.host)); @@ -169,7 +170,9 @@ export async function prefetchEmojis( select: ["name", "host", "originalUrl", "publicUrl"], }) : []; + const trans = redisClient.multi(); for (const emoji of _emojis) { - cache.set(`${emoji.name} ${emoji.host}`, emoji); + cache.set(`${emoji.name} ${emoji.host}`, emoji, trans); } + await trans.exec(); } diff --git a/packages/backend/src/remote/activitypub/models/person.ts b/packages/backend/src/remote/activitypub/models/person.ts index f8208e6d7..c541e9ae5 100644 --- a/packages/backend/src/remote/activitypub/models/person.ts +++ b/packages/backend/src/remote/activitypub/models/person.ts @@ -135,14 +135,14 @@ export async function fetchPerson( ): Promise { if (typeof uri !== "string") throw new Error("uri is not string"); - const cached = uriPersonCache.get(uri); + const cached = await uriPersonCache.get(uri); if (cached) return cached; // Fetch from the database if the URI points to this server if (uri.startsWith(`${config.url}/`)) { const id = uri.split("/").pop(); const u = await Users.findOneBy({ id }); - if (u) uriPersonCache.set(uri, u); + if (u) await uriPersonCache.set(uri, u); return u; } @@ -150,7 +150,7 @@ export async function fetchPerson( const exist = await Users.findOneBy({ uri }); if (exist) { - uriPersonCache.set(uri, exist); + await uriPersonCache.set(uri, exist); return exist; } //#endregion diff --git a/packages/backend/src/services/chart/charts/active-users.ts b/packages/backend/src/services/chart/charts/active-users.ts index 7a0c45cfa..15317e68b 100644 --- a/packages/backend/src/services/chart/charts/active-users.ts +++ b/packages/backend/src/services/chart/charts/active-users.ts @@ -25,12 +25,12 @@ export default class ActiveUsersChart extends Chart { return {}; } - public async read(user: { + public read(user: { id: User["id"]; host: null; createdAt: User["createdAt"]; - }): Promise { - await this.commit({ + }) { + this.commit({ read: [user.id], registeredWithinWeek: Date.now() - user.createdAt.getTime() < week ? [user.id] : [], diff --git a/packages/backend/src/services/instance-actor.ts b/packages/backend/src/services/instance-actor.ts index 50ce227eb..9240f3107 100644 --- a/packages/backend/src/services/instance-actor.ts +++ b/packages/backend/src/services/instance-actor.ts @@ -9,7 +9,7 @@ const ACTOR_USERNAME = "instance.actor" as const; const cache = new Cache(Infinity); export async function getInstanceActor(): Promise { - const cached = cache.get(null); + const cached = await cache.get(null); if (cached) return cached; const user = (await Users.findOneBy({ @@ -18,11 +18,11 @@ export async function getInstanceActor(): Promise { })) as ILocalUser | undefined; if (user) { - cache.set(null, user); + await cache.set(null, user); return user; } else { const created = (await createSystemUser(ACTOR_USERNAME)) as ILocalUser; - cache.set(null, created); + await cache.set(null, created); return created; } } 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 4c3570e90..ddb9ce241 100644 --- a/packages/backend/src/services/register-or-fetch-instance-doc.ts +++ b/packages/backend/src/services/register-or-fetch-instance-doc.ts @@ -9,25 +9,25 @@ const cache = new Cache(1000 * 60 * 60); export async function registerOrFetchInstanceDoc( host: string, ): Promise { - host = toPuny(host); + const _host = toPuny(host); - const cached = cache.get(host); + const cached = await cache.get(_host); if (cached) return cached; - const index = await Instances.findOneBy({ host }); + const index = await Instances.findOneBy({ host: _host }); if (index == null) { const i = await Instances.insert({ id: genId(), - host, + host: _host, caughtAt: new Date(), lastCommunicatedAt: new Date(), }).then((x) => Instances.findOneByOrFail(x.identifiers[0])); - cache.set(host, i); + await cache.set(_host, i); return i; } else { - cache.set(host, index); + await cache.set(_host, index); return index; } } diff --git a/packages/backend/src/services/relay.ts b/packages/backend/src/services/relay.ts index bec4b1f86..2325f76c6 100644 --- a/packages/backend/src/services/relay.ts +++ b/packages/backend/src/services/relay.ts @@ -90,7 +90,7 @@ async function updateRelaysCache() { const relays = await Relays.findBy({ status: "accepted", }); - relaysCache.set(null, relays); + await relaysCache.set(null, relays); } export async function relayRejected(id: string) { diff --git a/packages/backend/src/services/user-cache.ts b/packages/backend/src/services/user-cache.ts index 949244855..373fb8686 100644 --- a/packages/backend/src/services/user-cache.ts +++ b/packages/backend/src/services/user-cache.ts @@ -6,7 +6,7 @@ import type { import { 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 { redisClient, subscriber } from "@/db/redis.js"; export const userByIdCache = new Cache(Infinity); export const localUserByNativeTokenCache = new Cache( @@ -22,13 +22,12 @@ subscriber.on("message", async (_, data) => { const { type, body } = obj.message; switch (type) { case "localUserUpdated": { - userByIdCache.delete(body.id); - localUserByIdCache.delete(body.id); - localUserByNativeTokenCache.cache.forEach((v, k) => { - if (v.value?.id === body.id) { - localUserByNativeTokenCache.delete(k); - } - }); + await userByIdCache.delete(body.id); + await localUserByIdCache.delete(body.id); + const toDelete = Array.from(await localUserByNativeTokenCache.getAll()) + .filter((v) => v[1]?.id === body.id) + .map((v) => v[0]); + await localUserByNativeTokenCache.delete(...toDelete); break; } case "userChangeSuspendedState": @@ -36,15 +35,17 @@ subscriber.on("message", async (_, data) => { case "userChangeModeratorState": case "remoteUserUpdated": { const user = await Users.findOneByOrFail({ id: body.id }); - userByIdCache.set(user.id, user); - for (const [k, v] of uriPersonCache.cache.entries()) { - if (v.value?.id === user.id) { - uriPersonCache.set(k, user); + await userByIdCache.set(user.id, user); + const trans = redisClient.multi(); + for (const [k, v] of (await uriPersonCache.getAll()).entries()) { + if (v?.id === user.id) { + await uriPersonCache.set(k, user, trans); } } + await trans.exec(); if (Users.isLocalUser(user)) { - localUserByNativeTokenCache.set(user.token, user); - localUserByIdCache.set(user.id, user); + await localUserByNativeTokenCache.set(user.token, user); + await localUserByIdCache.set(user.id, user); } break; } @@ -52,8 +53,8 @@ subscriber.on("message", async (_, data) => { const user = (await Users.findOneByOrFail({ id: body.id, })) as ILocalUser; - localUserByNativeTokenCache.delete(body.oldToken); - localUserByNativeTokenCache.set(body.newToken, user); + await localUserByNativeTokenCache.delete(body.oldToken); + await localUserByNativeTokenCache.set(body.newToken, user); break; } default: diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1002a6f95..560bb55a3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -105,6 +105,9 @@ importers: '@koa/router': specifier: 9.0.1 version: 9.0.1 + '@msgpack/msgpack': + specifier: 3.0.0-beta2 + version: 3.0.0-beta2 '@peertube/http-signature': specifier: 1.7.0 version: 1.7.0 @@ -132,9 +135,6 @@ importers: argon2: specifier: ^0.30.3 version: 0.30.3 - async-mutex: - specifier: ^0.4.0 - version: 0.4.0 autobind-decorator: specifier: 2.4.0 version: 2.4.0 @@ -786,7 +786,7 @@ importers: version: 2.30.0 emojilib: specifier: github:thatonecalculator/emojilib - version: github.com/thatonecalculator/emojilib/542fcc1a25003afad78f3248ceee8ac6980ddeb8 + version: github.com/thatonecalculator/emojilib/06944984a61ee799b7083894258f5fa318d932d1 escape-regexp: specifier: 0.0.1 version: 0.0.1 @@ -2277,6 +2277,11 @@ packages: os-filter-obj: 2.0.0 dev: true + /@msgpack/msgpack@3.0.0-beta2: + resolution: {integrity: sha512-y+l1PNV0XDyY8sM3YtuMLK5vE3/hkfId+Do8pLo/OPxfxuFAUwcGz3oiiUuV46/aBpwTzZ+mRWVMtlSKbradhw==} + engines: {node: '>= 14'} + dev: false + /@msgpackr-extract/msgpackr-extract-darwin-arm64@3.0.2: resolution: {integrity: sha512-9bfjwDxIDWmmOKusUcqdS4Rw+SETlp9Dy39Xui9BEGEk19dDwH0jhipwFzEff/pFg95NKymc6TOTbRKcWeRqyQ==} cpu: [arm64] @@ -4496,12 +4501,6 @@ packages: stream-exhaust: 1.0.2 dev: true - /async-mutex@0.4.0: - resolution: {integrity: sha512-eJFZ1YhRR8UN8eBLoNzcDPcy/jqjsg6I1AP+KvWQX80BqOSW1oJPJXDylPUEeMr2ZQvHgnQ//Lp6f3RQ1zI7HA==} - dependencies: - tslib: 2.6.0 - dev: false - /async-settle@1.0.0: resolution: {integrity: sha512-VPXfB4Vk49z1LHHodrEQ6Xf7W4gg1w0dAPROHngx7qgDjqmIQ+fXmwgGXTW/ITLai0YLSvWepJOP9EVpMnEAcw==} engines: {node: '>= 0.10'} @@ -15772,8 +15771,8 @@ packages: url-polyfill: 1.1.12 dev: true - github.com/thatonecalculator/emojilib/542fcc1a25003afad78f3248ceee8ac6980ddeb8: - resolution: {tarball: https://codeload.github.com/thatonecalculator/emojilib/tar.gz/542fcc1a25003afad78f3248ceee8ac6980ddeb8} + github.com/thatonecalculator/emojilib/06944984a61ee799b7083894258f5fa318d932d1: + resolution: {tarball: https://codeload.github.com/thatonecalculator/emojilib/tar.gz/06944984a61ee799b7083894258f5fa318d932d1} name: emojilib version: 3.0.10 dev: true From 9d26e0803207a8b0c0024fc95a5b6ea2c45f17c1 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Sun, 2 Jul 2023 20:55:20 -0400 Subject: [PATCH 2/5] 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 d790313d1..c2695aa78 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 358fba0f3..c422cca94 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 45364bdcb..d2d15411f 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 4551bfd98..b0e07c4ab 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 ce25dd559..e6e6c2fb9 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 48c8d75b3..2bd8d4fba 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 6e448d4b1..4b4ea9627 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 42274ad2a..b6fa973eb 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 dbfb28ff6..28cefd2cf 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 9240f3107..1822cb3c7 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 f00678ce2..ca0f05f2c 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 ddb9ce241..e0c288037 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 2325f76c6..1ec2891e8 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 373fb8686..7fde21d87 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); From 284c0db1fde488ea5b0259701666790b599845d3 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Sun, 2 Jul 2023 22:10:33 -0400 Subject: [PATCH 3/5] no more infinity caches --- packages/backend/src/misc/cache.ts | 36 ++++++++++++------ .../backend/src/misc/check-hit-antenna.ts | 2 +- packages/backend/src/misc/emoji-meta.ts | 2 +- packages/backend/src/misc/keypair-store.ts | 8 ++-- packages/backend/src/misc/populate-emojis.ts | 2 +- .../backend/src/models/repositories/user.ts | 6 ++- .../src/remote/activitypub/db-resolver.ts | 38 ++++++++++++------- .../src/remote/activitypub/models/person.ts | 2 +- .../backend/src/server/api/authenticate.ts | 10 +++-- packages/backend/src/server/nodeinfo.ts | 5 ++- .../backend/src/services/instance-actor.ts | 4 +- packages/backend/src/services/note/create.ts | 7 +--- .../register-or-fetch-instance-doc.ts | 2 +- packages/backend/src/services/relay.ts | 2 +- packages/backend/src/services/user-cache.ts | 14 +++++-- 15 files changed, 89 insertions(+), 51 deletions(-) diff --git a/packages/backend/src/misc/cache.ts b/packages/backend/src/misc/cache.ts index c2695aa78..588931ff1 100644 --- a/packages/backend/src/misc/cache.ts +++ b/packages/backend/src/misc/cache.ts @@ -6,8 +6,8 @@ export class Cache { private ttl: number; private prefix: string; - constructor(prefix: string, ttl: number) { - this.ttl = ttl; + constructor(prefix: string, ttlSeconds: number) { + this.ttl = ttlSeconds; this.prefix = `cache:${prefix}`; } @@ -15,26 +15,28 @@ export class Cache { return key ? `${this.prefix}:${key}` : this.prefix; } - public async set(key: string | null, value: T, transaction?: ChainableCommander): Promise { + public async set( + key: string | null, + value: T, + transaction?: ChainableCommander, + ): Promise { const _key = this.prefixedKey(key); const _value = Buffer.from(encode(value)); const commander = transaction ?? redisClient; - if (this.ttl === Infinity) { - await commander.set(_key, _value); - } else { - await commander.set(_key, _value, "PX", this.ttl); - } + await commander.set(_key, _value, "EX", this.ttl); } - public async get(key: string | null): Promise { + public async get(key: string | null, renew = false): Promise { const _key = this.prefixedKey(key); const cached = await redisClient.getBuffer(_key); if (cached === null) return undefined; + if (renew) await redisClient.expire(_key, this.ttl); + return decode(cached) as T; } - public async getAll(): Promise> { + public async getAll(renew = false): Promise> { const keys = await redisClient.keys(`${this.prefix}*`); const map = new Map(); if (keys.length === 0) { @@ -49,6 +51,14 @@ export class Cache { } } + if (renew) { + const trans = redisClient.multi(); + for (const key of map.keys()) { + trans.expire(key, this.ttl); + } + await trans.exec(); + } + return map; } @@ -66,9 +76,10 @@ export class Cache { public async fetch( key: string | null, fetcher: () => Promise, + renew = false, validator?: (cachedValue: T) => boolean, ): Promise { - const cachedValue = await this.get(key); + const cachedValue = await this.get(key, renew); if (cachedValue !== undefined) { if (validator) { if (validator(cachedValue)) { @@ -94,9 +105,10 @@ export class Cache { public async fetchMaybe( key: string | null, fetcher: () => Promise, + renew = false, validator?: (cachedValue: T) => boolean, ): Promise { - const cachedValue = await this.get(key); + const cachedValue = await this.get(key, renew); if (cachedValue !== undefined) { if (validator) { if (validator(cachedValue)) { diff --git a/packages/backend/src/misc/check-hit-antenna.ts b/packages/backend/src/misc/check-hit-antenna.ts index c422cca94..1ff09d629 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("blocking", 1000 * 60 * 5); +const blockingCache = new Cache("blocking", 60 * 5); // NOTE: フォローしているユーザーのノート、リストのユーザーのノート、グループのユーザーのノート指定はパフォーマンス上の理由で無効になっている diff --git a/packages/backend/src/misc/emoji-meta.ts b/packages/backend/src/misc/emoji-meta.ts index d2d15411f..2b9365b82 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("emojiMeta",1000 * 60 * 10); // once every 10 minutes for the same url +const cache = new Cache("emojiMeta", 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 b0e07c4ab..625577359 100644 --- a/packages/backend/src/misc/keypair-store.ts +++ b/packages/backend/src/misc/keypair-store.ts @@ -3,10 +3,12 @@ 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("keypairStore", Infinity); +const cache = new Cache("keypairStore", 60 * 30); export async function getUserKeypair(userId: User["id"]): Promise { - return await cache.fetch(userId, () => - UserKeypairs.findOneByOrFail({ userId: userId }), + return await cache.fetch( + userId, + () => UserKeypairs.findOneByOrFail({ userId: userId }), + true, ); } diff --git a/packages/backend/src/misc/populate-emojis.ts b/packages/backend/src/misc/populate-emojis.ts index e6e6c2fb9..795a267f9 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("populateEmojis", 1000 * 60 * 60 * 12); +const cache = new Cache("populateEmojis", 60 * 60 * 12); /** * 添付用絵文字情報 diff --git a/packages/backend/src/models/repositories/user.ts b/packages/backend/src/models/repositories/user.ts index 2bd8d4fba..5ca36e3d3 100644 --- a/packages/backend/src/models/repositories/user.ts +++ b/packages/backend/src/models/repositories/user.ts @@ -1,4 +1,3 @@ -import { URL } from "url"; import { In, Not } from "typeorm"; import Ajv from "ajv"; import type { ILocalUser, IRemoteUser } from "@/models/entities/user.js"; @@ -40,7 +39,10 @@ import { } from "../index.js"; import type { Instance } from "../entities/instance.js"; -const userInstanceCache = new Cache("userInstance", 1000 * 60 * 60 * 3); +const userInstanceCache = new Cache( + "userInstance", + 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 4b4ea9627..a710b9f11 100644 --- a/packages/backend/src/remote/activitypub/db-resolver.ts +++ b/packages/backend/src/remote/activitypub/db-resolver.ts @@ -5,7 +5,6 @@ import type { CacheableRemoteUser, CacheableUser, } from "@/models/entities/user.js"; -import { User, IRemoteUser } from "@/models/entities/user.js"; import type { UserPublickey } from "@/models/entities/user-publickey.js"; import type { MessagingMessage } from "@/models/entities/messaging-message.js"; import { @@ -20,8 +19,11 @@ import type { IObject } from "./type.js"; import { getApId } from "./type.js"; import { resolvePerson } from "./models/person.js"; -const publicKeyCache = new Cache("publicKey", Infinity); -const publicKeyByUserIdCache = new Cache("publicKeyByUserId", Infinity); +const publicKeyCache = new Cache("publicKey", 60 * 30); +const publicKeyByUserIdCache = new Cache( + "publicKeyByUserId", + 60 * 30, +); export type UriParseResult = | { @@ -123,17 +125,23 @@ export default class DbResolver { if (parsed.type !== "users") return null; return ( - (await userByIdCache.fetchMaybe(parsed.id, () => - Users.findOneBy({ - id: parsed.id, - }).then((x) => x ?? undefined), + (await userByIdCache.fetchMaybe( + parsed.id, + () => + Users.findOneBy({ + id: parsed.id, + }).then((x) => x ?? undefined), + true, )) ?? null ); } else { - return await uriPersonCache.fetch(parsed.uri, () => - Users.findOneBy({ - uri: parsed.uri, - }), + return await uriPersonCache.fetch( + parsed.uri, + () => + Users.findOneBy({ + uri: parsed.uri, + }), + true, ); } } @@ -156,14 +164,17 @@ export default class DbResolver { return key; }, + true, (key) => key != null, ); if (key == null) return null; return { - user: (await userByIdCache.fetch(key.userId, () => - Users.findOneByOrFail({ id: key.userId }), + user: (await userByIdCache.fetch( + key.userId, + () => Users.findOneByOrFail({ id: key.userId }), + true, )) as CacheableRemoteUser, key, }; @@ -183,6 +194,7 @@ export default class DbResolver { const key = await publicKeyByUserIdCache.fetch( user.id, () => UserPublickeys.findOneBy({ userId: user.id }), + true, (v) => v != null, ); diff --git a/packages/backend/src/remote/activitypub/models/person.ts b/packages/backend/src/remote/activitypub/models/person.ts index c541e9ae5..c5519ba03 100644 --- a/packages/backend/src/remote/activitypub/models/person.ts +++ b/packages/backend/src/remote/activitypub/models/person.ts @@ -135,7 +135,7 @@ export async function fetchPerson( ): Promise { if (typeof uri !== "string") throw new Error("uri is not string"); - const cached = await uriPersonCache.get(uri); + const cached = await uriPersonCache.get(uri, true); if (cached) return cached; // Fetch from the database if the URI points to this server diff --git a/packages/backend/src/server/api/authenticate.ts b/packages/backend/src/server/api/authenticate.ts index b6fa973eb..460a0ce84 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("app", Infinity); +const appCache = new Cache("app", 60 * 30); export class AuthenticationError extends Error { constructor(message: string) { @@ -49,6 +49,7 @@ export default async ( const user = await localUserByNativeTokenCache.fetch( token, () => Users.findOneBy({ token }) as Promise, + true, ); if (user == null) { @@ -82,11 +83,14 @@ export default async ( Users.findOneBy({ id: accessToken.userId, }) as Promise, + true, ); if (accessToken.appId) { - const app = await appCache.fetch(accessToken.appId, () => - Apps.findOneByOrFail({ id: accessToken.appId! }), + const app = await appCache.fetch( + accessToken.appId, + () => Apps.findOneByOrFail({ id: accessToken.appId! }), + true, ); return [ diff --git a/packages/backend/src/server/nodeinfo.ts b/packages/backend/src/server/nodeinfo.ts index 28cefd2cf..940ca2e13 100644 --- a/packages/backend/src/server/nodeinfo.ts +++ b/packages/backend/src/server/nodeinfo.ts @@ -100,7 +100,10 @@ const nodeinfo2 = async () => { }; }; -const cache = new Cache>>("nodeinfo", 1000 * 60 * 10); +const cache = new Cache>>( + "nodeinfo", + 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 1822cb3c7..a8b34ea57 100644 --- a/packages/backend/src/services/instance-actor.ts +++ b/packages/backend/src/services/instance-actor.ts @@ -6,10 +6,10 @@ import { IsNull } from "typeorm"; const ACTOR_USERNAME = "instance.actor" as const; -const cache = new Cache("instanceActor", Infinity); +const cache = new Cache("instanceActor", 60 * 30); export async function getInstanceActor(): Promise { - const cached = await cache.get(null); + const cached = await cache.get(null, true); if (cached) return cached; const user = (await Users.findOneBy({ diff --git a/packages/backend/src/services/note/create.ts b/packages/backend/src/services/note/create.ts index ca0f05f2c..095c75f42 100644 --- a/packages/backend/src/services/note/create.ts +++ b/packages/backend/src/services/note/create.ts @@ -29,17 +29,14 @@ import { Notes, Instances, UserProfiles, - Antennas, - Followings, MutedNotes, Channels, ChannelFollowings, - Blockings, NoteThreadMutings, } from "@/models/index.js"; import type { DriveFile } from "@/models/entities/drive-file.js"; import type { App } from "@/models/entities/app.js"; -import { Not, In, IsNull } from "typeorm"; +import { Not, In } from "typeorm"; import type { User, ILocalUser, IRemoteUser } from "@/models/entities/user.js"; import { genId } from "@/misc/gen-id.js"; import { @@ -73,7 +70,7 @@ import { Mutex } from "redis-semaphore"; const mutedWordsCache = new Cache< { userId: UserProfile["userId"]; mutedWords: UserProfile["mutedWords"] }[] ->("mutedWords", 1000 * 60 * 5); +>("mutedWords", 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 e0c288037..c0ead0819 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("registerOrFetchInstanceDoc", 1000 * 60 * 60); +const cache = new Cache("registerOrFetchInstanceDoc", 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 1ec2891e8..6f7829c21 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("relay", 1000 * 60 * 10); +const relaysCache = new Cache("relay", 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 7fde21d87..ed700185d 100644 --- a/packages/backend/src/services/user-cache.ts +++ b/packages/backend/src/services/user-cache.ts @@ -7,13 +7,19 @@ import { Users } from "@/models/index.js"; import { Cache } from "@/misc/cache.js"; import { redisClient, subscriber } from "@/db/redis.js"; -export const userByIdCache = new Cache("userById", Infinity); +export const userByIdCache = new Cache("userById", 60 * 30); export const localUserByNativeTokenCache = new Cache( "localUserByNativeToken", - Infinity, + 60 * 30, +); +export const localUserByIdCache = new Cache( + "localUserByIdCache", + 60 * 30, +); +export const uriPersonCache = new Cache( + "uriPerson", + 60 * 30, ); -export const localUserByIdCache = new Cache("localUserByIdCache", Infinity); -export const uriPersonCache = new Cache("uriPerson", Infinity); subscriber.on("message", async (_, data) => { const obj = JSON.parse(data); From b36cc31e9b9858693bb36731aef4fd58a5facc12 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Sun, 2 Jul 2023 22:21:19 -0400 Subject: [PATCH 4/5] throw error if failed --- .../backend/src/server/api/endpoints/admin/emoji/add.ts | 9 ++------- .../backend/src/server/api/endpoints/admin/emoji/copy.ts | 9 ++------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/add.ts b/packages/backend/src/server/api/endpoints/admin/emoji/add.ts index 7d4081613..4366406ec 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/add.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/add.ts @@ -6,7 +6,7 @@ import { ApiError } from "../../../error.js"; import rndstr from "rndstr"; import { publishBroadcastStream } from "@/services/stream.js"; import { db } from "@/db/postgre.js"; -import { type Size, getEmojiSize } from "@/misc/emoji-meta.js"; +import { getEmojiSize } from "@/misc/emoji-meta.js"; export const meta = { tags: ["admin"], @@ -40,12 +40,7 @@ export default define(meta, paramDef, async (ps, me) => { ? file.name.split(".")[0] : `_${rndstr("a-z0-9", 8)}_`; - let size: Size = { width: 0, height: 0 }; - try { - size = await getEmojiSize(file.url); - } catch { - /* skip if any error happens */ - } + const size = await getEmojiSize(file.url); const emoji = await Emojis.insert({ id: genId(), diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts b/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts index 45cb9464d..c90e60633 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts @@ -6,7 +6,7 @@ import type { DriveFile } from "@/models/entities/drive-file.js"; import { uploadFromUrl } from "@/services/drive/upload-from-url.js"; import { publishBroadcastStream } from "@/services/stream.js"; import { db } from "@/db/postgre.js"; -import { type Size, getEmojiSize } from "@/misc/emoji-meta.js"; +import { getEmojiSize } from "@/misc/emoji-meta.js"; export const meta = { tags: ["admin"], @@ -65,12 +65,7 @@ export default define(meta, paramDef, async (ps, me) => { throw new ApiError(); } - let size: Size = { width: 0, height: 0 }; - try { - size = await getEmojiSize(driveFile.url); - } catch { - /* skip if any error happens */ - } + const size = await getEmojiSize(driveFile.url); const copied = await Emojis.insert({ id: genId(), From 6f5e07de5d6a5f976969313b90c9108e84fc1258 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Sun, 2 Jul 2023 23:14:28 -0400 Subject: [PATCH 5/5] rename arg --- packages/backend/src/misc/cache.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/misc/cache.ts b/packages/backend/src/misc/cache.ts index 588931ff1..fe68908e5 100644 --- a/packages/backend/src/misc/cache.ts +++ b/packages/backend/src/misc/cache.ts @@ -6,9 +6,9 @@ export class Cache { private ttl: number; private prefix: string; - constructor(prefix: string, ttlSeconds: number) { + constructor(name: string, ttlSeconds: number) { this.ttl = ttlSeconds; - this.prefix = `cache:${prefix}`; + this.prefix = `cache:${name}`; } private prefixedKey(key: string | null): string {