From 2570f8a9eb03289f0bbf2ad79c36f028679fbbfd Mon Sep 17 00:00:00 2001 From: Namekuji Date: Tue, 5 Sep 2023 09:32:57 -0400 Subject: [PATCH] further cleanup --- packages/backend/src/db/scylla.ts | 21 ++++++++++----- .../backend/src/server/api/endpoints/stats.ts | 6 ++--- .../src/server/api/mastodon/endpoints/meta.ts | 2 +- packages/backend/src/server/nodeinfo.ts | 2 +- packages/backend/src/services/note/create.ts | 27 ++++++++++++++++++- 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/packages/backend/src/db/scylla.ts b/packages/backend/src/db/scylla.ts index 7e0e822b9a..1cac68603b 100644 --- a/packages/backend/src/db/scylla.ts +++ b/packages/backend/src/db/scylla.ts @@ -65,14 +65,11 @@ function newClient(): Client | null { } export const scyllaClient = newClient(); - export const prepared = scyllaQueries; -const localPostCountCache = new Cache("localPostCount", 1000 * 60 * 10); -export const allPostCountCache = new Cache( - "allPostCount", - 1000 * 60 * 10, -); +const localPostCountCache = new Cache("localPostCount", 1000 * 60 * 60); +const allPostCountCache = new Cache("allPostCount", 1000 * 60 * 60); +const reactionCountCache = new Cache("reactionCount", 1000 * 60 * 60); export async function fetchPostCount(local = false): Promise { if (!scyllaClient) { @@ -94,6 +91,18 @@ export async function fetchPostCount(local = false): Promise { ); } +export async function fetchReactionCount(): Promise { + if (!scyllaClient) { + throw new Error("ScyllaDB is disabled"); + } + + return await reactionCountCache.fetch(null, () => + scyllaClient + .execute("SELECT COUNT(*) FROM reaction") + .then((result) => result.first().get("count") as number), + ); +} + export interface ScyllaNotification { targetId: string; createdAtDate: Date; diff --git a/packages/backend/src/server/api/endpoints/stats.ts b/packages/backend/src/server/api/endpoints/stats.ts index 97889c42ed..65631df833 100644 --- a/packages/backend/src/server/api/endpoints/stats.ts +++ b/packages/backend/src/server/api/endpoints/stats.ts @@ -1,7 +1,7 @@ -import { Instances, NoteReactions, Notes, Users } from "@/models/index.js"; +import { Instances, NoteReactions } from "@/models/index.js"; import define from "../define.js"; import { driveChart, notesChart, usersChart } from "@/services/chart/index.js"; -import { IsNull } from "typeorm"; +import { fetchReactionCount, scyllaClient } from "@/db/scylla.js"; export const meta = { requireCredential: false, @@ -79,7 +79,7 @@ export default define(meta, paramDef, async () => { //originalReactionsCount, instances, ] = await Promise.all([ - NoteReactions.count({ cache: 3600000 }), // 1 hour + scyllaClient ? fetchReactionCount() : NoteReactions.count({ cache: 3600000 }), // 1 hour //NoteReactions.count({ where: { userHost: IsNull() }, cache: 3600000 }), Instances.count({ cache: 3600000 }), ]); diff --git a/packages/backend/src/server/api/mastodon/endpoints/meta.ts b/packages/backend/src/server/api/mastodon/endpoints/meta.ts index 5940084556..ec6c505e27 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/meta.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/meta.ts @@ -4,7 +4,7 @@ import { fetchMeta } from "@/misc/fetch-meta.js"; import { Users, Notes } from "@/models/index.js"; import { IsNull } from "typeorm"; import { MAX_NOTE_TEXT_LENGTH, FILE_TYPE_BROWSERSAFE } from "@/const.js"; -import { fetchPostCount, scyllaClient } from "@/db/scylla"; +import { fetchPostCount, scyllaClient } from "@/db/scylla.js"; export async function getInstance( response: Entity.Instance, diff --git a/packages/backend/src/server/nodeinfo.ts b/packages/backend/src/server/nodeinfo.ts index 6bde5cab45..04606c53f0 100644 --- a/packages/backend/src/server/nodeinfo.ts +++ b/packages/backend/src/server/nodeinfo.ts @@ -5,7 +5,7 @@ import { Users, Notes } from "@/models/index.js"; import { IsNull, MoreThan } from "typeorm"; import { MAX_NOTE_TEXT_LENGTH, MAX_CAPTION_TEXT_LENGTH } from "@/const.js"; import { Cache } from "@/misc/cache.js"; -import { fetchPostCount, scyllaClient } from "@/db/scylla"; +import { fetchPostCount, scyllaClient } from "@/db/scylla.js"; const router = new Router(); diff --git a/packages/backend/src/services/note/create.ts b/packages/backend/src/services/note/create.ts index 05320882ca..ed0a86134e 100644 --- a/packages/backend/src/services/note/create.ts +++ b/packages/backend/src/services/note/create.ts @@ -38,7 +38,12 @@ import { import type { DriveFile } from "@/models/entities/drive-file.js"; import type { App } from "@/models/entities/app.js"; import { Not, In } from "typeorm"; -import type { User, ILocalUser, IRemoteUser } from "@/models/entities/user.js"; +import type { + User, + ILocalUser, + IRemoteUser, + CacheableUser, +} from "@/models/entities/user.js"; import { genId } from "@/misc/gen-id.js"; import { notesChart, @@ -75,6 +80,7 @@ import { scyllaClient, ScyllaPoll, } from "@/db/scylla.js"; +import { userByIdCache, userDenormalizedCache } from "../user-cache.js"; export const mutedWordsCache = new Cache< { userId: UserProfile["userId"]; mutedWords: UserProfile["mutedWords"] }[] @@ -1136,6 +1142,25 @@ function incNotesCountOfUser(user: { id: User["id"] }) { }) .where("id = :id", { id: user.id }) .execute(); + + userByIdCache.get(user.id).then((cache) => { + if (cache) { + const newUser: CacheableUser = { + ...cache, + notesCount: cache.notesCount + 1, + }; + userByIdCache.set(user.id, newUser); + } + }); + userDenormalizedCache.get(user.id).then((cache) => { + if (cache) { + const newUser: CacheableUser = { + ...cache, + notesCount: cache.notesCount + 1, + }; + userDenormalizedCache.set(user.id, newUser); + } + }); } export async function extractMentionedUsers(