server: make fetcher key non-null

This commit is contained in:
Norm 2022-12-07 17:10:54 -05:00
parent b23a8dbaed
commit cbfd866122
4 changed files with 11 additions and 11 deletions

View file

@ -1,7 +1,7 @@
export class Cache<T> { export class Cache<T> {
public cache: Map<string | null, { date: number; value: T; }>; public cache: Map<string, { date: number; value: T; }>;
private lifetime: number; private lifetime: number;
public fetcher: (key: string | null) => Promise<T | undefined>; public fetcher: (key: string) => Promise<T | undefined>;
constructor(lifetime: number, fetcher: Cache<T>['fetcher']) { constructor(lifetime: number, fetcher: Cache<T>['fetcher']) {
this.cache = new Map(); this.cache = new Map();
@ -9,14 +9,14 @@ export class Cache<T> {
this.fetcher = fetcher; this.fetcher = fetcher;
} }
public set(key: string | null, value: T): void { public set(key: string, value: T): void {
this.cache.set(key, { this.cache.set(key, {
date: Date.now(), date: Date.now(),
value, value,
}); });
} }
public get(key: string | null): T | undefined { public get(key: string): T | undefined {
const cached = this.cache.get(key); const cached = this.cache.get(key);
if (cached == null) return undefined; if (cached == null) return undefined;
@ -29,7 +29,7 @@ export class Cache<T> {
return cached.value; return cached.value;
} }
public delete(key: string | null): void { public delete(key: string): void {
this.cache.delete(key); this.cache.delete(key);
} }
@ -38,7 +38,7 @@ export class Cache<T> {
* run to get the value. If the fetcher returns undefined, it is * run to get the value. If the fetcher returns undefined, it is
* returned but not cached. * returned but not cached.
*/ */
public async fetch(key: string | null): Promise<T | undefined> { public async fetch(key: string): Promise<T | undefined> {
const cached = this.get(key); const cached = this.get(key);
if (cached !== undefined) { if (cached !== undefined) {
return cached; return cached;

View file

@ -266,7 +266,7 @@ export default async (user: { id: User['id']; username: User['username']; host:
incNotesCountOfUser(user); incNotesCountOfUser(user);
// Word mute // Word mute
mutedWordsCache.fetch(null).then(us => { mutedWordsCache.fetch('').then(us => {
for (const u of us) { for (const u of us) {
checkWordMute(note, { id: u.userId }, u.mutedWords).then(shouldMute => { checkWordMute(note, { id: u.userId }, u.mutedWords).then(shouldMute => {
if (shouldMute) { if (shouldMute) {

View file

@ -93,7 +93,7 @@ export async function relayRejected(id: string): Promise<string> {
export async function deliverToRelays(user: { id: User['id']; host: null; }, activity: any): Promise<void> { export async function deliverToRelays(user: { id: User['id']; host: null; }, activity: any): Promise<void> {
if (activity == null) return; if (activity == null) return;
const relays = await relaysCache.fetch(null); const relays = await relaysCache.fetch('');
if (relays == null || relays.length === 0) return; if (relays == null || relays.length === 0) return;
// TODO // TODO

View file

@ -6,15 +6,15 @@ import { subscriber } from '@/db/redis.js';
export const userByIdCache = new Cache<CacheableUser>( export const userByIdCache = new Cache<CacheableUser>(
Infinity, Infinity,
async (id) => id ? (await Users.findOneBy({ id }) ?? undefined) : undefined, async (id) => await Users.findOneBy({ id }) ?? undefined,
); );
export const localUserByNativeTokenCache = new Cache<CacheableLocalUser>( export const localUserByNativeTokenCache = new Cache<CacheableLocalUser>(
Infinity, Infinity,
async (token) => token ? (await Users.findOneBy({ token, host: IsNull() }) as ILocalUser | null ?? undefined) : undefined, async (token) => await Users.findOneBy({ token, host: IsNull() }) as ILocalUser | null ?? undefined,
); );
export const uriPersonCache = new Cache<CacheableUser>( export const uriPersonCache = new Cache<CacheableUser>(
Infinity, Infinity,
async (uri) => uri ? (await Users.findOneBy({ uri }) ?? undefined) : undefined, async (uri) => await Users.findOneBy({ uri }) ?? undefined,
); );
subscriber.on('message', async (_, data) => { subscriber.on('message', async (_, data) => {