add cache prefix

This commit is contained in:
Namekuji 2023-07-02 20:55:20 -04:00
parent ef724a49fe
commit 9d26e08032
14 changed files with 22 additions and 23 deletions

View file

@ -1,19 +1,18 @@
import { redisClient } from "@/db/redis.js"; import { redisClient } from "@/db/redis.js";
import { nativeRandomStr } from "native-utils/built/index.js";
import { encode, decode } from "@msgpack/msgpack"; import { encode, decode } from "@msgpack/msgpack";
import { ChainableCommander } from "ioredis"; import { ChainableCommander } from "ioredis";
export class Cache<T> { export class Cache<T> {
private ttl: number; private ttl: number;
private fingerprint: string; private prefix: string;
constructor(ttl: number) { constructor(prefix: string, ttl: number) {
this.ttl = ttl; this.ttl = ttl;
this.fingerprint = `cache:${nativeRandomStr(32)}`; this.prefix = `cache:${prefix}`;
} }
private prefixedKey(key: string | null): string { 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<void> { public async set(key: string | null, value: T, transaction?: ChainableCommander): Promise<void> {
@ -36,7 +35,7 @@ export class Cache<T> {
} }
public async getAll(): Promise<Map<string, T>> { public async getAll(): Promise<Map<string, T>> {
const keys = await redisClient.keys(`${this.fingerprint}*`); const keys = await redisClient.keys(`${this.prefix}*`);
const map = new Map<string, T>(); const map = new Map<string, T>();
if (keys.length === 0) { if (keys.length === 0) {
return map; return map;

View file

@ -11,7 +11,7 @@ import * as Acct from "@/misc/acct.js";
import type { Packed } from "./schema.js"; import type { Packed } from "./schema.js";
import { Cache } from "./cache.js"; import { Cache } from "./cache.js";
const blockingCache = new Cache<User["id"][]>(1000 * 60 * 5); const blockingCache = new Cache<User["id"][]>("blocking", 1000 * 60 * 5);
// NOTE: フォローしているユーザーのノート、リストのユーザーのノート、グループのユーザーのノート指定はパフォーマンス上の理由で無効になっている // NOTE: フォローしているユーザーのノート、リストのユーザーのノート、グループのユーザーのノート指定はパフォーマンス上の理由で無効になっている

View file

@ -11,7 +11,7 @@ export type Size = {
height: number; height: number;
}; };
const cache = new Cache<boolean>(1000 * 60 * 10); // once every 10 minutes for the same url const cache = new Cache<boolean>("emojiMeta",1000 * 60 * 10); // once every 10 minutes for the same url
const logger = new Logger("emoji"); const logger = new Logger("emoji");
export async function getEmojiSize(url: string): Promise<Size> { export async function getEmojiSize(url: string): Promise<Size> {

View file

@ -3,7 +3,7 @@ import type { User } from "@/models/entities/user.js";
import type { UserKeypair } from "@/models/entities/user-keypair.js"; import type { UserKeypair } from "@/models/entities/user-keypair.js";
import { Cache } from "./cache.js"; import { Cache } from "./cache.js";
const cache = new Cache<UserKeypair>(Infinity); const cache = new Cache<UserKeypair>("keypairStore", Infinity);
export async function getUserKeypair(userId: User["id"]): Promise<UserKeypair> { export async function getUserKeypair(userId: User["id"]): Promise<UserKeypair> {
return await cache.fetch(userId, () => return await cache.fetch(userId, () =>

View file

@ -9,7 +9,7 @@ import config from "@/config/index.js";
import { query } from "@/prelude/url.js"; import { query } from "@/prelude/url.js";
import { redisClient } from "@/db/redis.js"; import { redisClient } from "@/db/redis.js";
const cache = new Cache<Emoji | null>(1000 * 60 * 60 * 12); const cache = new Cache<Emoji | null>("populateEmojis", 1000 * 60 * 60 * 12);
/** /**
* *

View file

@ -40,7 +40,7 @@ import {
} from "../index.js"; } from "../index.js";
import type { Instance } from "../entities/instance.js"; import type { Instance } from "../entities/instance.js";
const userInstanceCache = new Cache<Instance | null>(1000 * 60 * 60 * 3); const userInstanceCache = new Cache<Instance | null>("userInstance", 1000 * 60 * 60 * 3);
type IsUserDetailed<Detailed extends boolean> = Detailed extends true type IsUserDetailed<Detailed extends boolean> = Detailed extends true
? Packed<"UserDetailed"> ? Packed<"UserDetailed">

View file

@ -20,8 +20,8 @@ import type { IObject } from "./type.js";
import { getApId } from "./type.js"; import { getApId } from "./type.js";
import { resolvePerson } from "./models/person.js"; import { resolvePerson } from "./models/person.js";
const publicKeyCache = new Cache<UserPublickey | null>(Infinity); const publicKeyCache = new Cache<UserPublickey | null>("publicKey", Infinity);
const publicKeyByUserIdCache = new Cache<UserPublickey | null>(Infinity); const publicKeyByUserIdCache = new Cache<UserPublickey | null>("publicKeyByUserId", Infinity);
export type UriParseResult = export type UriParseResult =
| { | {

View file

@ -9,7 +9,7 @@ import {
localUserByNativeTokenCache, localUserByNativeTokenCache,
} from "@/services/user-cache.js"; } from "@/services/user-cache.js";
const appCache = new Cache<App>(Infinity); const appCache = new Cache<App>("app", Infinity);
export class AuthenticationError extends Error { export class AuthenticationError extends Error {
constructor(message: string) { constructor(message: string) {

View file

@ -100,7 +100,7 @@ const nodeinfo2 = async () => {
}; };
}; };
const cache = new Cache<Awaited<ReturnType<typeof nodeinfo2>>>(1000 * 60 * 10); const cache = new Cache<Awaited<ReturnType<typeof nodeinfo2>>>("nodeinfo", 1000 * 60 * 10);
router.get(nodeinfo2_1path, async (ctx) => { router.get(nodeinfo2_1path, async (ctx) => {
const base = await cache.fetch(null, () => nodeinfo2()); const base = await cache.fetch(null, () => nodeinfo2());

View file

@ -6,7 +6,7 @@ import { IsNull } from "typeorm";
const ACTOR_USERNAME = "instance.actor" as const; const ACTOR_USERNAME = "instance.actor" as const;
const cache = new Cache<ILocalUser>(Infinity); const cache = new Cache<ILocalUser>("instanceActor", Infinity);
export async function getInstanceActor(): Promise<ILocalUser> { export async function getInstanceActor(): Promise<ILocalUser> {
const cached = await cache.get(null); const cached = await cache.get(null);

View file

@ -73,7 +73,7 @@ import { Mutex } from "redis-semaphore";
const mutedWordsCache = new Cache< const mutedWordsCache = new Cache<
{ userId: UserProfile["userId"]; mutedWords: UserProfile["mutedWords"] }[] { userId: UserProfile["userId"]; mutedWords: UserProfile["mutedWords"] }[]
>(1000 * 60 * 5); >("mutedWords", 1000 * 60 * 5);
type NotificationType = "reply" | "renote" | "quote" | "mention"; type NotificationType = "reply" | "renote" | "quote" | "mention";

View file

@ -4,7 +4,7 @@ import { genId } from "@/misc/gen-id.js";
import { toPuny } from "@/misc/convert-host.js"; import { toPuny } from "@/misc/convert-host.js";
import { Cache } from "@/misc/cache.js"; import { Cache } from "@/misc/cache.js";
const cache = new Cache<Instance>(1000 * 60 * 60); const cache = new Cache<Instance>("registerOrFetchInstanceDoc", 1000 * 60 * 60);
export async function registerOrFetchInstanceDoc( export async function registerOrFetchInstanceDoc(
host: string, host: string,

View file

@ -15,7 +15,7 @@ import { createSystemUser } from "./create-system-user.js";
const ACTOR_USERNAME = "relay.actor" as const; const ACTOR_USERNAME = "relay.actor" as const;
const relaysCache = new Cache<Relay[]>(1000 * 60 * 10); const relaysCache = new Cache<Relay[]>("relay", 1000 * 60 * 10);
export async function getRelayActor(): Promise<ILocalUser> { export async function getRelayActor(): Promise<ILocalUser> {
const user = await Users.findOneBy({ const user = await Users.findOneBy({

View file

@ -3,17 +3,17 @@ import type {
CacheableUser, CacheableUser,
ILocalUser, ILocalUser,
} from "@/models/entities/user.js"; } from "@/models/entities/user.js";
import { User } from "@/models/entities/user.js";
import { Users } from "@/models/index.js"; import { Users } from "@/models/index.js";
import { Cache } from "@/misc/cache.js"; import { Cache } from "@/misc/cache.js";
import { redisClient, subscriber } from "@/db/redis.js"; import { redisClient, subscriber } from "@/db/redis.js";
export const userByIdCache = new Cache<CacheableUser>(Infinity); export const userByIdCache = new Cache<CacheableUser>("userById", Infinity);
export const localUserByNativeTokenCache = new Cache<CacheableLocalUser | null>( export const localUserByNativeTokenCache = new Cache<CacheableLocalUser | null>(
"localUserByNativeToken",
Infinity, Infinity,
); );
export const localUserByIdCache = new Cache<CacheableLocalUser>(Infinity); export const localUserByIdCache = new Cache<CacheableLocalUser>("localUserByIdCache", Infinity);
export const uriPersonCache = new Cache<CacheableUser | null>(Infinity); export const uriPersonCache = new Cache<CacheableUser | null>("uriPerson", Infinity);
subscriber.on("message", async (_, data) => { subscriber.on("message", async (_, data) => {
const obj = JSON.parse(data); const obj = JSON.parse(data);