summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/NotificationService.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2023-04-04 17:32:09 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2023-04-04 17:32:09 +0900
commitecaf152b4a6eb702375debaef0dddc2cca798116 (patch)
tree85b2ca34a67ad66368fea84d0462b5c109b22415 /packages/backend/src/core/NotificationService.ts
parentrefactor(backend): rename Cache -> MemoryCache (diff)
downloadsharkey-ecaf152b4a6eb702375debaef0dddc2cca798116.tar.gz
sharkey-ecaf152b4a6eb702375debaef0dddc2cca798116.tar.bz2
sharkey-ecaf152b4a6eb702375debaef0dddc2cca798116.zip
enhance(backend): improve cache
Diffstat (limited to 'packages/backend/src/core/NotificationService.ts')
-rw-r--r--packages/backend/src/core/NotificationService.ts18
1 files changed, 7 insertions, 11 deletions
diff --git a/packages/backend/src/core/NotificationService.ts b/packages/backend/src/core/NotificationService.ts
index 2a4dbba6a4..9c179f9318 100644
--- a/packages/backend/src/core/NotificationService.ts
+++ b/packages/backend/src/core/NotificationService.ts
@@ -3,7 +3,7 @@ import Redis from 'ioredis';
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
import { In } from 'typeorm';
import { DI } from '@/di-symbols.js';
-import type { MutingsRepository, UserProfilesRepository, UsersRepository } from '@/models/index.js';
+import type { MutingsRepository, UserProfile, UserProfilesRepository, UsersRepository } from '@/models/index.js';
import type { User } from '@/models/entities/User.js';
import type { Notification } from '@/models/entities/Notification.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
@@ -12,6 +12,7 @@ import { GlobalEventService } from '@/core/GlobalEventService.js';
import { PushNotificationService } from '@/core/PushNotificationService.js';
import { NotificationEntityService } from '@/core/entities/NotificationEntityService.js';
import { IdService } from '@/core/IdService.js';
+import { CacheService } from '@/core/CacheService.js';
@Injectable()
export class NotificationService implements OnApplicationShutdown {
@@ -35,6 +36,7 @@ export class NotificationService implements OnApplicationShutdown {
private idService: IdService,
private globalEventService: GlobalEventService,
private pushNotificationService: PushNotificationService,
+ private cacheService: CacheService,
) {
}
@@ -49,7 +51,6 @@ export class NotificationService implements OnApplicationShutdown {
'+',
'-',
'COUNT', 1);
- console.log('latestNotificationIdsRes', latestNotificationIdsRes);
const latestNotificationId = latestNotificationIdsRes[0]?.[0];
if (latestNotificationId == null) return;
@@ -72,9 +73,8 @@ export class NotificationService implements OnApplicationShutdown {
type: Notification['type'],
data: Partial<Notification>,
): Promise<Notification | null> {
- // TODO: Cache
- const profile = await this.userProfilesRepository.findOneBy({ userId: notifieeId });
- const isMuted = profile?.mutingNotificationTypes.includes(type);
+ const profile = await this.cacheService.userProfileCache.fetch(notifieeId, () => this.userProfilesRepository.findOneByOrFail({ userId: notifieeId }));
+ const isMuted = profile.mutingNotificationTypes.includes(type);
if (isMuted) return null;
if (data.notifierId) {
@@ -82,12 +82,8 @@ export class NotificationService implements OnApplicationShutdown {
return null;
}
- // TODO: cache
- const mutings = await this.mutingsRepository.findOneBy({
- muterId: notifieeId,
- muteeId: data.notifierId,
- });
- if (mutings) {
+ const mutings = await this.cacheService.userMutingsCache.fetch(notifieeId, () => this.mutingsRepository.findBy({ muterId: notifieeId }).then(xs => xs.map(x => x.muteeId)));
+ if (mutings.includes(data.notifierId)) {
return null;
}
}