summaryrefslogtreecommitdiff
path: root/packages/backend/src/misc
diff options
context:
space:
mode:
authorsyuilo <4439005+syuilo@users.noreply.github.com>2024-02-16 15:07:12 +0900
committersyuilo <4439005+syuilo@users.noreply.github.com>2024-02-16 15:07:12 +0900
commitf55e1ee13897ed2dadb6dff8af965255a178c724 (patch)
tree4bcd452431006c2fcfde52edbddf733588c8628b /packages/backend/src/misc
parentリモートユーザーが復活してもキャッシュにより該当ユ... (diff)
downloadmisskey-f55e1ee13897ed2dadb6dff8af965255a178c724.tar.gz
misskey-f55e1ee13897ed2dadb6dff8af965255a178c724.tar.bz2
misskey-f55e1ee13897ed2dadb6dff8af965255a178c724.zip
refactor(backend): misc/cacheをシンプルな実装に戻した
Diffstat (limited to 'packages/backend/src/misc')
-rw-r--r--packages/backend/src/misc/cache.ts36
1 files changed, 10 insertions, 26 deletions
diff --git a/packages/backend/src/misc/cache.ts b/packages/backend/src/misc/cache.ts
index e0f7ea6ed9..4cde6e12d3 100644
--- a/packages/backend/src/misc/cache.ts
+++ b/packages/backend/src/misc/cache.ts
@@ -186,28 +186,14 @@ export class RedisSingleCache<T> {
// TODO: メモリ節約のためあまり参照されないキーを定期的に削除できるようにする?
-function nothingToDo<T, V = T>(value: T): V {
- return value as unknown as V;
-}
-
-export class MemoryKVCache<T, V = T> {
- public cache: Map<string, { date: number; value: V; }>;
+export class MemoryKVCache<T> {
+ public cache: Map<string, { date: number; value: T; }>;
private lifetime: number;
- private gcIntervalHandle: NodeJS.Timeout;
- private toMapConverter: (value: T) => V;
- private fromMapConverter: (cached: V) => T | undefined;
+ private gcIntervalHandle: NodeJS.Timer;
- constructor(lifetime: MemoryKVCache<never>['lifetime'], options: {
- toMapConverter: (value: T) => V;
- fromMapConverter: (cached: V) => T | undefined;
- } = {
- toMapConverter: nothingToDo,
- fromMapConverter: nothingToDo,
- }) {
+ constructor(lifetime: MemoryKVCache<never>['lifetime']) {
this.cache = new Map();
this.lifetime = lifetime;
- this.toMapConverter = options.toMapConverter;
- this.fromMapConverter = options.fromMapConverter;
this.gcIntervalHandle = setInterval(() => {
this.gc();
@@ -218,7 +204,7 @@ export class MemoryKVCache<T, V = T> {
public set(key: string, value: T): void {
this.cache.set(key, {
date: Date.now(),
- value: this.toMapConverter(value),
+ value,
});
}
@@ -230,7 +216,7 @@ export class MemoryKVCache<T, V = T> {
this.cache.delete(key);
return undefined;
}
- return this.fromMapConverter(cached.value);
+ return cached.value;
}
@bindThis
@@ -241,10 +227,9 @@ export class MemoryKVCache<T, V = T> {
/**
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
- * fetcherの引数はcacheに保存されている値があれば渡されます
*/
@bindThis
- public async fetch(key: string, fetcher: (value: V | undefined) => Promise<T>, validator?: (cachedValue: T) => boolean): Promise<T> {
+ public async fetch(key: string, fetcher: () => Promise<T>, validator?: (cachedValue: T) => boolean): Promise<T> {
const cachedValue = this.get(key);
if (cachedValue !== undefined) {
if (validator) {
@@ -259,7 +244,7 @@ export class MemoryKVCache<T, V = T> {
}
// Cache MISS
- const value = await fetcher(this.cache.get(key)?.value);
+ const value = await fetcher();
this.set(key, value);
return value;
}
@@ -267,10 +252,9 @@ export class MemoryKVCache<T, V = T> {
/**
* キャッシュがあればそれを返し、無ければfetcherを呼び出して結果をキャッシュ&返します
* optional: キャッシュが存在してもvalidatorでfalseを返すとキャッシュ無効扱いにします
- * fetcherの引数はcacheに保存されている値があれば渡されます
*/
@bindThis
- public async fetchMaybe(key: string, fetcher: (value: V | undefined) => Promise<T | undefined>, validator?: (cachedValue: T) => boolean): Promise<T | undefined> {
+ public async fetchMaybe(key: string, fetcher: () => Promise<T | undefined>, validator?: (cachedValue: T) => boolean): Promise<T | undefined> {
const cachedValue = this.get(key);
if (cachedValue !== undefined) {
if (validator) {
@@ -285,7 +269,7 @@ export class MemoryKVCache<T, V = T> {
}
// Cache MISS
- const value = await fetcher(this.cache.get(key)?.value);
+ const value = await fetcher();
if (value !== undefined) {
this.set(key, value);
}