manage push subscriptions cache

This commit is contained in:
Hazelnoot 2025-10-01 12:27:22 -04:00
parent 2caec2f530
commit ad5443d2cc

View file

@ -12,8 +12,7 @@ import type { Packed } from '@/misc/json-schema.js';
import { getNoteSummary } from '@/misc/get-note-summary.js';
import type { MiMeta, MiSwSubscription, SwSubscriptionsRepository } from '@/models/_.js';
import { bindThis } from '@/decorators.js';
import { QuantumKVCache } from '@/misc/QuantumKVCache.js';
import { InternalEventService } from '@/core/InternalEventService.js';
import { CacheManagementService, type ManagedQuantumKVCache } from '@/core/CacheManagementService.js';
// Defined also packages/sw/types.ts#L13
type PushNotificationsTypes = {
@ -48,8 +47,8 @@ function truncateBody<T extends keyof PushNotificationsTypes>(type: T, body: Pus
}
@Injectable()
export class PushNotificationService implements OnApplicationShutdown {
private subscriptionsCache: QuantumKVCache<MiSwSubscription[]>;
export class PushNotificationService {
private readonly subscriptionsCache: ManagedQuantumKVCache<MiSwSubscription[]>;
constructor(
@Inject(DI.config)
@ -63,9 +62,9 @@ export class PushNotificationService implements OnApplicationShutdown {
@Inject(DI.swSubscriptionsRepository)
private swSubscriptionsRepository: SwSubscriptionsRepository,
private readonly internalEventService: InternalEventService,
cacheManagementService: CacheManagementService,
) {
this.subscriptionsCache = new QuantumKVCache<MiSwSubscription[]>(this.internalEventService, 'userSwSubscriptions', {
this.subscriptionsCache = cacheManagementService.createQuantumKVCache<MiSwSubscription[]>('userSwSubscriptions', {
lifetime: 1000 * 60 * 60 * 1, // 1h
fetcher: (key) => this.swSubscriptionsRepository.findBy({ userId: key }),
});
@ -125,14 +124,4 @@ export class PushNotificationService implements OnApplicationShutdown {
public async refreshCache(userId: string): Promise<void> {
await this.subscriptionsCache.refresh(userId);
}
@bindThis
public dispose(): void {
this.subscriptionsCache.dispose();
}
@bindThis
public onApplicationShutdown(signal?: string | undefined): void {
this.dispose();
}
}