manage push subscriptions cache
This commit is contained in:
parent
2caec2f530
commit
ad5443d2cc
1 changed files with 5 additions and 16 deletions
|
|
@ -12,8 +12,7 @@ import type { Packed } from '@/misc/json-schema.js';
|
||||||
import { getNoteSummary } from '@/misc/get-note-summary.js';
|
import { getNoteSummary } from '@/misc/get-note-summary.js';
|
||||||
import type { MiMeta, MiSwSubscription, SwSubscriptionsRepository } from '@/models/_.js';
|
import type { MiMeta, MiSwSubscription, SwSubscriptionsRepository } from '@/models/_.js';
|
||||||
import { bindThis } from '@/decorators.js';
|
import { bindThis } from '@/decorators.js';
|
||||||
import { QuantumKVCache } from '@/misc/QuantumKVCache.js';
|
import { CacheManagementService, type ManagedQuantumKVCache } from '@/core/CacheManagementService.js';
|
||||||
import { InternalEventService } from '@/core/InternalEventService.js';
|
|
||||||
|
|
||||||
// Defined also packages/sw/types.ts#L13
|
// Defined also packages/sw/types.ts#L13
|
||||||
type PushNotificationsTypes = {
|
type PushNotificationsTypes = {
|
||||||
|
|
@ -48,8 +47,8 @@ function truncateBody<T extends keyof PushNotificationsTypes>(type: T, body: Pus
|
||||||
}
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PushNotificationService implements OnApplicationShutdown {
|
export class PushNotificationService {
|
||||||
private subscriptionsCache: QuantumKVCache<MiSwSubscription[]>;
|
private readonly subscriptionsCache: ManagedQuantumKVCache<MiSwSubscription[]>;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(DI.config)
|
@Inject(DI.config)
|
||||||
|
|
@ -63,9 +62,9 @@ export class PushNotificationService implements OnApplicationShutdown {
|
||||||
|
|
||||||
@Inject(DI.swSubscriptionsRepository)
|
@Inject(DI.swSubscriptionsRepository)
|
||||||
private swSubscriptionsRepository: 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
|
lifetime: 1000 * 60 * 60 * 1, // 1h
|
||||||
fetcher: (key) => this.swSubscriptionsRepository.findBy({ userId: key }),
|
fetcher: (key) => this.swSubscriptionsRepository.findBy({ userId: key }),
|
||||||
});
|
});
|
||||||
|
|
@ -125,14 +124,4 @@ export class PushNotificationService implements OnApplicationShutdown {
|
||||||
public async refreshCache(userId: string): Promise<void> {
|
public async refreshCache(userId: string): Promise<void> {
|
||||||
await this.subscriptionsCache.refresh(userId);
|
await this.subscriptionsCache.refresh(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
|
||||||
public dispose(): void {
|
|
||||||
this.subscriptionsCache.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
@bindThis
|
|
||||||
public onApplicationShutdown(signal?: string | undefined): void {
|
|
||||||
this.dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue