pass services into caches through constructor

This commit is contained in:
Hazelnoot 2025-10-01 11:35:24 -04:00
parent 4116c19e7e
commit 72da199f61
2 changed files with 83 additions and 38 deletions

View file

@ -5,8 +5,25 @@
import * as Redis from 'ioredis';
import { bindThis } from '@/decorators.js';
import { TimeService, NativeTimeService } from '@/core/TimeService.js';
// This matches the default DI implementation, but as a shared instance to avoid wasting memory.
const defaultTimeService: TimeService = new NativeTimeService();
export interface RedisCacheServices extends MemoryCacheServices {
readonly redisClient: Redis.Redis
}
export interface RedisKVCacheOpts<T> {
lifetime: number;
memoryCacheLifetime: number;
fetcher?: RedisKVCache<T>['fetcher'];
toRedisConverter?: RedisKVCache<T>['toRedisConverter'];
fromRedisConverter?: RedisKVCache<T>['fromRedisConverter'];
}
export class RedisKVCache<T> {
private readonly redisClient: Redis.Redis;
private readonly lifetime: number;
private readonly memoryCache: MemoryKVCache<T>;
public readonly fetcher: (key: string) => Promise<T>;
@ -14,18 +31,13 @@ export class RedisKVCache<T> {
public readonly fromRedisConverter: (value: string) => T | undefined;
constructor(
private redisClient: Redis.Redis,
private name: string,
opts: {
lifetime: RedisKVCache<T>['lifetime'];
memoryCacheLifetime: number;
fetcher?: RedisKVCache<T>['fetcher'];
toRedisConverter?: RedisKVCache<T>['toRedisConverter'];
fromRedisConverter?: RedisKVCache<T>['fromRedisConverter'];
},
public name: string,
services: RedisCacheServices,
opts: RedisKVCacheOpts<T>,
) {
this.redisClient = services.redisClient;
this.lifetime = opts.lifetime;
this.memoryCache = new MemoryKVCache(opts.memoryCacheLifetime);
this.memoryCache = new MemoryKVCache(opts.memoryCacheLifetime, services);
this.fetcher = opts.fetcher ?? (() => { throw new Error('fetch not supported - use get/set directly'); });
this.toRedisConverter = opts.toRedisConverter ?? ((value) => JSON.stringify(value));
this.fromRedisConverter = opts.fromRedisConverter ?? ((value) => JSON.parse(value));
@ -115,7 +127,16 @@ export class RedisKVCache<T> {
}
}
export interface RedisSingleCacheOpts<T> {
lifetime: number;
memoryCacheLifetime: number;
fetcher?: RedisSingleCache<T>['fetcher'];
toRedisConverter?: RedisSingleCache<T>['toRedisConverter'];
fromRedisConverter?: RedisSingleCache<T>['fromRedisConverter'];
}
export class RedisSingleCache<T> {
private readonly redisClient: Redis.Redis;
private readonly lifetime: number;
private readonly memoryCache: MemorySingleCache<T>;
public readonly fetcher: () => Promise<T>;
@ -123,18 +144,13 @@ export class RedisSingleCache<T> {
public readonly fromRedisConverter: (value: string) => T | undefined;
constructor(
private redisClient: Redis.Redis,
private name: string,
opts: {
lifetime: number;
memoryCacheLifetime: number;
fetcher?: RedisSingleCache<T>['fetcher'];
toRedisConverter?: RedisSingleCache<T>['toRedisConverter'];
fromRedisConverter?: RedisSingleCache<T>['fromRedisConverter'];
},
public name: string,
services: RedisCacheServices,
opts: RedisSingleCacheOpts<T>,
) {
this.redisClient = services.redisClient;
this.lifetime = opts.lifetime;
this.memoryCache = new MemorySingleCache(opts.memoryCacheLifetime);
this.memoryCache = new MemorySingleCache(opts.memoryCacheLifetime, services);
this.fetcher = opts.fetcher ?? (() => { throw new Error('fetch not supported - use get/set directly'); });
this.toRedisConverter = opts.toRedisConverter ?? ((value) => JSON.stringify(value));
@ -219,17 +235,25 @@ export class RedisSingleCache<T> {
this.clear();
}
}
export interface MemoryCacheServices {
readonly timeService?: TimeService;
}
// TODO: メモリ節約のためあまり参照されないキーを定期的に削除できるようにする?
export class MemoryKVCache<T> {
private readonly cache = new Map<string, { date: number; value: T; }>();
private readonly gcIntervalHandle = setInterval(() => this.gc(), 1000 * 60 * 3); // 3m
private readonly gcIntervalHandle: symbol;
private readonly timeService: TimeService;
constructor(
private readonly lifetime: number,
) {}
services?: MemoryCacheServices,
) {
this.timeService = services?.timeService ?? defaultTimeService;
this.gcIntervalHandle = this.timeService.startTimer(() => this.gc(), 1000 * 60 * 3, { repeated: true }); // 3m
}
@bindThis
/**
@ -238,7 +262,7 @@ export class MemoryKVCache<T> {
*/
public set(key: string, value: T): void {
this.cache.set(key, {
date: Date.now(),
date: this.timeService.now,
value,
});
}
@ -247,7 +271,7 @@ export class MemoryKVCache<T> {
public get(key: string): T | undefined {
const cached = this.cache.get(key);
if (cached == null) return undefined;
if ((Date.now() - cached.date) > this.lifetime) {
if ((this.timeService.now - cached.date) > this.lifetime) {
this.cache.delete(key);
return undefined;
}
@ -257,7 +281,7 @@ export class MemoryKVCache<T> {
public has(key: string): boolean {
const cached = this.cache.get(key);
if (cached == null) return false;
if ((Date.now() - cached.date) > this.lifetime) {
if ((this.timeService.now - cached.date) > this.lifetime) {
this.cache.delete(key);
return false;
}
@ -323,7 +347,7 @@ export class MemoryKVCache<T> {
@bindThis
public gc(): void {
const now = Date.now();
const now = this.timeService.now;
for (const [key, { date }] of this.cache.entries()) {
// The map is ordered from oldest to youngest.
@ -346,7 +370,7 @@ export class MemoryKVCache<T> {
@bindThis
public dispose(): void {
this.clear();
clearInterval(this.gcIntervalHandle);
this.timeService.stopTimer(this.gcIntervalHandle);
}
public get size() {
@ -359,23 +383,27 @@ export class MemoryKVCache<T> {
}
export class MemorySingleCache<T> {
private readonly timeService: TimeService;
private cachedAt: number | null = null;
private value: T | undefined;
constructor(
private lifetime: number,
) {}
services?: MemoryCacheServices,
) {
this.timeService = services?.timeService ?? defaultTimeService;
}
@bindThis
public set(value: T): void {
this.cachedAt = Date.now();
this.cachedAt = this.timeService.now;
this.value = value;
}
@bindThis
public get(): T | undefined {
if (this.cachedAt == null) return undefined;
if ((Date.now() - this.cachedAt) > this.lifetime) {
if ((this.timeService.now - this.cachedAt) > this.lifetime) {
this.value = undefined;
this.cachedAt = null;
return undefined;