manage sponsors cache

This commit is contained in:
Hazelnoot 2025-10-01 12:28:16 -04:00
parent 0e516126f7
commit 32ea83927f

View file

@ -3,12 +3,11 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import * as Redis from 'ioredis';
import type { MiMeta } from '@/models/_.js'; import type { MiMeta } from '@/models/_.js';
import { DI } from '@/di-symbols.js'; import { DI } from '@/di-symbols.js';
import { RedisKVCache } from '@/misc/cache.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { CacheManagementService, type ManagedRedisKVCache } from '@/core/CacheManagementService.js';
export interface Sponsor { export interface Sponsor {
MemberId: number; MemberId: number;
@ -34,17 +33,16 @@ export interface Sponsor {
} }
@Injectable() @Injectable()
export class SponsorsService implements OnApplicationShutdown { export class SponsorsService {
private readonly cache: RedisKVCache<Sponsor[]>; private readonly cache: ManagedRedisKVCache<Sponsor[]>;
constructor( constructor(
@Inject(DI.meta) @Inject(DI.meta)
private readonly meta: MiMeta, private readonly meta: MiMeta,
@Inject(DI.redis) cacheManagementService: CacheManagementService,
redisClient: Redis.Redis,
) { ) {
this.cache = new RedisKVCache<Sponsor[]>(redisClient, 'sponsors', { this.cache = cacheManagementService.createRedisKVCache<Sponsor[]>('sponsors', {
lifetime: 1000 * 60 * 60, lifetime: 1000 * 60 * 60,
memoryCacheLifetime: 1000 * 60, memoryCacheLifetime: 1000 * 60,
fetcher: (key) => { fetcher: (key) => {
@ -102,9 +100,4 @@ export class SponsorsService implements OnApplicationShutdown {
if (forceUpdate) await this.cache.refresh('sharkey'); if (forceUpdate) await this.cache.refresh('sharkey');
return this.cache.fetch('sharkey'); return this.cache.fetch('sharkey');
} }
@bindThis
public onApplicationShutdown(): void {
this.cache.dispose();
}
} }