mirror of
https://github.com/misskey-dev/misskey
synced 2024-12-01 08:18:51 +09:00
perf(backend): cache swSubscriptions
This commit is contained in:
parent
3a90bcc03c
commit
c10d591bd0
@ -1,12 +1,14 @@
|
|||||||
import { Inject, Injectable } from '@nestjs/common';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
import push from 'web-push';
|
import push from 'web-push';
|
||||||
|
import Redis from 'ioredis';
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
import type { Config } from '@/config.js';
|
import type { Config } from '@/config.js';
|
||||||
import type { Packed } from '@/misc/json-schema';
|
import type { Packed } from '@/misc/json-schema';
|
||||||
import { getNoteSummary } from '@/misc/get-note-summary.js';
|
import { getNoteSummary } from '@/misc/get-note-summary.js';
|
||||||
import type { SwSubscriptionsRepository } from '@/models/index.js';
|
import type { SwSubscription, SwSubscriptionsRepository } from '@/models/index.js';
|
||||||
import { MetaService } from '@/core/MetaService.js';
|
import { MetaService } from '@/core/MetaService.js';
|
||||||
import { bindThis } from '@/decorators.js';
|
import { bindThis } from '@/decorators.js';
|
||||||
|
import { RedisKVCache } from '@/misc/cache.js';
|
||||||
|
|
||||||
// Defined also packages/sw/types.ts#L13
|
// Defined also packages/sw/types.ts#L13
|
||||||
type PushNotificationsTypes = {
|
type PushNotificationsTypes = {
|
||||||
@ -41,15 +43,27 @@ function truncateBody<T extends keyof PushNotificationsTypes>(type: T, body: Pus
|
|||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PushNotificationService {
|
export class PushNotificationService {
|
||||||
|
private subscriptionsCache: RedisKVCache<SwSubscription[]>;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(DI.config)
|
@Inject(DI.config)
|
||||||
private config: Config,
|
private config: Config,
|
||||||
|
|
||||||
|
@Inject(DI.redis)
|
||||||
|
private redisClient: Redis.Redis,
|
||||||
|
|
||||||
@Inject(DI.swSubscriptionsRepository)
|
@Inject(DI.swSubscriptionsRepository)
|
||||||
private swSubscriptionsRepository: SwSubscriptionsRepository,
|
private swSubscriptionsRepository: SwSubscriptionsRepository,
|
||||||
|
|
||||||
private metaService: MetaService,
|
private metaService: MetaService,
|
||||||
) {
|
) {
|
||||||
|
this.subscriptionsCache = new RedisKVCache<SwSubscription[]>(this.redisClient, 'userSwSubscriptions', {
|
||||||
|
lifetime: 1000 * 60 * 60 * 1, // 1h
|
||||||
|
memoryCacheLifetime: 1000 * 60 * 3, // 3m
|
||||||
|
fetcher: (key) => this.swSubscriptionsRepository.findBy({ userId: key }),
|
||||||
|
toRedisConverter: (value) => JSON.stringify(value),
|
||||||
|
fromRedisConverter: (value) => JSON.parse(value),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
@ -63,10 +77,7 @@ export class PushNotificationService {
|
|||||||
meta.swPublicKey,
|
meta.swPublicKey,
|
||||||
meta.swPrivateKey);
|
meta.swPrivateKey);
|
||||||
|
|
||||||
// Fetch
|
const subscriptions = await this.subscriptionsCache.fetch(userId);
|
||||||
const subscriptions = await this.swSubscriptionsRepository.findBy({
|
|
||||||
userId: userId,
|
|
||||||
});
|
|
||||||
|
|
||||||
for (const subscription of subscriptions) {
|
for (const subscription of subscriptions) {
|
||||||
if ([
|
if ([
|
||||||
|
Loading…
Reference in New Issue
Block a user