Compare commits
2 commits
451c674906
...
101ea21747
Author | SHA1 | Date | |
---|---|---|---|
101ea21747 | |||
8d78113907 |
3 changed files with 68 additions and 18 deletions
|
@ -1,12 +1,23 @@
|
|||
export class Cache<T> {
|
||||
// The actual "database" that holds the cache entries, along with their
|
||||
// insertion time.
|
||||
// Insertion order is the same as the order of elements expiring. This is
|
||||
// important because the expiration logic relies on the insertion order.
|
||||
public cache: Map<string, { date: number; value: T; }>;
|
||||
// The lifetime of each cache member.
|
||||
//
|
||||
// This must not be changed after setup because it may upset
|
||||
// the expiration logic.
|
||||
private lifetime: number;
|
||||
// Function of which the results should be cached.
|
||||
public fetcher: (key: string) => Promise<T | undefined>;
|
||||
private timeoutScheduled: boolean;
|
||||
|
||||
constructor(lifetime: number, fetcher: Cache<T>['fetcher']) {
|
||||
this.cache = new Map();
|
||||
this.lifetime = lifetime;
|
||||
this.fetcher = fetcher;
|
||||
this.timeoutScheduled = false;
|
||||
}
|
||||
|
||||
public set(key: string, value: T): void {
|
||||
|
@ -14,38 +25,36 @@ export class Cache<T> {
|
|||
date: Date.now(),
|
||||
value,
|
||||
});
|
||||
|
||||
// make sure the expiration timeout is in place
|
||||
this.expire();
|
||||
}
|
||||
|
||||
public get(key: string): T | undefined {
|
||||
const cached = this.cache.get(key);
|
||||
if (cached == null) return undefined;
|
||||
|
||||
// discard if past the cache lifetime
|
||||
if ((Date.now() - cached.date) > this.lifetime) {
|
||||
this.cache.delete(key);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return cached.value;
|
||||
else return cached.value;
|
||||
}
|
||||
|
||||
public delete(key: string): void {
|
||||
this.cache.delete(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* If the value is cached, it is returned. Otherwise the fetcher is
|
||||
* run to get the value. If the fetcher returns undefined, it is
|
||||
* returned but not cached.
|
||||
*/
|
||||
// If the value is cached, it is returned. Otherwise the fetcher is
|
||||
// run to get the value. If the fetcher returns undefined, it is
|
||||
// returned but not cached.
|
||||
public async fetch(key: string): Promise<T | undefined> {
|
||||
// Check if this value is cached
|
||||
const cached = this.get(key);
|
||||
if (cached !== undefined) {
|
||||
// The value was cached, return it.
|
||||
return cached;
|
||||
} else {
|
||||
// The value was not cached, need to call the original function
|
||||
// to get its result and then cache it.
|
||||
const value = await this.fetcher(key);
|
||||
|
||||
// don't cache undefined
|
||||
// `undefined` is not cached
|
||||
if (value !== undefined) {
|
||||
this.set(key, value);
|
||||
}
|
||||
|
@ -53,4 +62,43 @@ export class Cache<T> {
|
|||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
// Handling the expiration of cached values.
|
||||
// This is done using a timeout.
|
||||
private expire(): void {
|
||||
// If there already is a timeout scheduled, it will be appropriate
|
||||
// for the first inserted element of the cache.
|
||||
// If the first element of the cache was removed, it will reschedule
|
||||
// to the appropriate time when it runs out.
|
||||
//
|
||||
// If the cache is empty, there is nothing to expire either.
|
||||
if (this.timeoutScheduled) return;
|
||||
// Otherwise, this must mean this is the previously scheduled timeout.
|
||||
// Since it is running now, it is no longer scheduled.
|
||||
this.timeoutScheduled = false;
|
||||
|
||||
// Check if the first element is actually due for expiration.
|
||||
//
|
||||
// Items may have been removed in the meantime or this may be
|
||||
// the initial call for the first key inserted into the cache.
|
||||
const [expiredKey, expiredValue] = this.cache.entries().next().value;
|
||||
if (expiredValue.date + this.lifetime >= Date.now()) {
|
||||
// This item is due for expiration, so remove it.
|
||||
this.cache.delete(expiredKey);
|
||||
}
|
||||
|
||||
// If there are no further elements in the cache, there is nothing to
|
||||
// expire at a later time. The timeout will be set up again later by
|
||||
// a call from `this.set`.
|
||||
if (this.cache.size === 0) return;
|
||||
|
||||
// Check when the next key is due for removal and schedule
|
||||
// an appropriate timeout.
|
||||
const [nextKey, nextValue] = this.cache.entries().next().value;
|
||||
setTimeout(
|
||||
() => this.expire(),
|
||||
nextValue.date + this.lifetime - Date.now()
|
||||
);
|
||||
this.timeoutScheduled = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,9 +2,10 @@ import { UserKeypairs } from '@/models/index.js';
|
|||
import { User } from '@/models/entities/user.js';
|
||||
import { UserKeypair } from '@/models/entities/user-keypair.js';
|
||||
import { Cache } from './cache.js';
|
||||
import { MINUTE } from '@/const.js';
|
||||
|
||||
const cache = new Cache<UserKeypair>(
|
||||
Infinity,
|
||||
15 * MINUTE,
|
||||
(userId) => UserKeypairs.findOneByOrFail({ userId }),
|
||||
);
|
||||
|
||||
|
|
|
@ -3,17 +3,18 @@ import { ILocalUser, User } from '@/models/entities/user.js';
|
|||
import { Users } from '@/models/index.js';
|
||||
import { Cache } from '@/misc/cache.js';
|
||||
import { subscriber } from '@/db/redis.js';
|
||||
import { MINUTE } from '@/const.js';
|
||||
|
||||
export const userByIdCache = new Cache<User>(
|
||||
Infinity,
|
||||
15 * MINUTE,
|
||||
async (id) => await Users.findOneBy({ id, isDeleted: IsNull() }) ?? undefined,
|
||||
);
|
||||
export const localUserByNativeTokenCache = new Cache<ILocalUser>(
|
||||
Infinity,
|
||||
15 * MINUTE,
|
||||
async (token) => await Users.findOneBy({ token, host: IsNull(), isDeleted: IsNull() }) as ILocalUser | null ?? undefined,
|
||||
);
|
||||
export const uriPersonCache = new Cache<User>(
|
||||
Infinity,
|
||||
15 * MINUTE,
|
||||
async (uri) => await Users.findOneBy({ uri, isDeleted: IsNull() }) ?? undefined,
|
||||
);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue