add cache prefix

This commit is contained in:
Namekuji 2023-07-02 20:55:20 -04:00
parent 947163fde2
commit 76c9422d53
No known key found for this signature in database
GPG Key ID: 1D62332C07FBA532
14 changed files with 22 additions and 23 deletions

View File

@ -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<T> {
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<void> {
@ -36,7 +35,7 @@ export class Cache<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>();
if (keys.length === 0) {
return map;

View File

@ -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<User["id"][]>(1000 * 60 * 5);
const blockingCache = new Cache<User["id"][]>("blocking", 1000 * 60 * 5);
// NOTE: フォローしているユーザーのノート、リストのユーザーのノート、グループのユーザーのノート指定はパフォーマンス上の理由で無効になっている

View File

@ -11,7 +11,7 @@ export type Size = {
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");
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 { 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> {
return await cache.fetch(userId, () =>

View File

@ -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<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";
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
? Packed<"UserDetailed">

View File

@ -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<UserPublickey | null>(Infinity);
const publicKeyByUserIdCache = new Cache<UserPublickey | null>(Infinity);
const publicKeyCache = new Cache<UserPublickey | null>("publicKey", Infinity);
const publicKeyByUserIdCache = new Cache<UserPublickey | null>("publicKeyByUserId", Infinity);
export type UriParseResult =
| {

View File

@ -9,7 +9,7 @@ import {
localUserByNativeTokenCache,
} from "@/services/user-cache.js";
const appCache = new Cache<App>(Infinity);
const appCache = new Cache<App>("app", Infinity);
export class AuthenticationError extends Error {
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) => {
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 cache = new Cache<ILocalUser>(Infinity);
const cache = new Cache<ILocalUser>("instanceActor", Infinity);
export async function getInstanceActor(): Promise<ILocalUser> {
const cached = await cache.get(null);

View File

@ -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";

View File

@ -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<Instance>(1000 * 60 * 60);
const cache = new Cache<Instance>("registerOrFetchInstanceDoc", 1000 * 60 * 60);
export async function registerOrFetchInstanceDoc(
host: string,

View File

@ -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[]>("relay", 1000 * 60 * 10);
export async function getRelayActor(): Promise<ILocalUser> {
const user = await Users.findOneBy({

View File

@ -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<CacheableUser>(Infinity);
export const userByIdCache = new Cache<CacheableUser>("userById", Infinity);
export const localUserByNativeTokenCache = new Cache<CacheableLocalUser | null>(
"localUserByNativeToken",
Infinity,
);
export const localUserByIdCache = new Cache<CacheableLocalUser>(Infinity);
export const uriPersonCache = new Cache<CacheableUser | null>(Infinity);
export const localUserByIdCache = new Cache<CacheableLocalUser>("localUserByIdCache", Infinity);
export const uriPersonCache = new Cache<CacheableUser | null>("uriPerson", Infinity);
subscriber.on("message", async (_, data) => {
const obj = JSON.parse(data);