further cleanup

This commit is contained in:
Namekuji 2023-09-05 09:32:57 -04:00
parent 98ffb20c6a
commit 2570f8a9eb
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532
5 changed files with 46 additions and 12 deletions

View file

@ -65,14 +65,11 @@ function newClient(): Client | null {
}
export const scyllaClient = newClient();
export const prepared = scyllaQueries;
const localPostCountCache = new Cache<number>("localPostCount", 1000 * 60 * 10);
export const allPostCountCache = new Cache<number>(
"allPostCount",
1000 * 60 * 10,
);
const localPostCountCache = new Cache<number>("localPostCount", 1000 * 60 * 60);
const allPostCountCache = new Cache<number>("allPostCount", 1000 * 60 * 60);
const reactionCountCache = new Cache<number>("reactionCount", 1000 * 60 * 60);
export async function fetchPostCount(local = false): Promise<number> {
if (!scyllaClient) {
@ -94,6 +91,18 @@ export async function fetchPostCount(local = false): Promise<number> {
);
}
export async function fetchReactionCount(): Promise<number> {
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;

View file

@ -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 }),
]);

View file

@ -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,

View file

@ -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();

View file

@ -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(