Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor cache functions #3079

Merged
merged 9 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions apps/api/src/app/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ import { ANALYTICS_SERVICE } from '../../shared/shared.module';
import { CachedEntity } from '../../shared/interceptors/cached-entity.interceptor';
import { normalizeEmail } from '../../shared/helpers/email-normalization.service';
import { ApiException } from '../../shared/exceptions/api.exception';
import { buildCommonKey, buildKeyById, CacheKeyPrefixEnum, CacheKeyTypeEnum } from '../../shared/services/cache/keys';
import {
buildEnvironmentByApiKey,
buildSubscriberKey,
buildUserKey,
} from '../../shared/services/cache/key-builders/entities';

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -287,10 +291,8 @@ export class AuthService {

@CachedEntity({
builder: (command: { _id: string }) =>
buildKeyById({
type: CacheKeyTypeEnum.ENTITY,
keyEntity: CacheKeyPrefixEnum.USER,
identifier: command._id,
buildUserKey({
_id: command._id,
Comment on lines +294 to +295
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good one.

}),
})
private async getUser({ _id }: { _id: string }) {
Expand All @@ -299,10 +301,8 @@ export class AuthService {

@CachedEntity({
builder: (command: { _id: string }) =>
buildKeyById({
type: CacheKeyTypeEnum.ENTITY,
keyEntity: CacheKeyPrefixEnum.ENVIRONMENT_BY_API_KEY,
identifier: command._id,
buildEnvironmentByApiKey({
_id: command._id,
}),
})
private async getEnvironment({ _id }: { _id: string }) {
Expand All @@ -311,12 +311,9 @@ export class AuthService {

@CachedEntity({
builder: (command: { subscriberId: string; _environmentId: string }) =>
buildCommonKey({
type: CacheKeyTypeEnum.ENTITY,
keyEntity: CacheKeyPrefixEnum.SUBSCRIBER,
environmentId: command._environmentId,
identifier: command.subscriberId,
identifierPrefix: 's',
buildSubscriberKey({
_environmentId: command._environmentId,
subscriberId: command.subscriberId,
}),
})
private async getSubscriber({ subscriberId, _environmentId }: { subscriberId: string; _environmentId: string }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { differenceInHours, differenceInSeconds, parseISO } from 'date-fns';
import { normalizeEmail } from '../../../shared/helpers/email-normalization.service';
import { PasswordResetRequestCommand } from './password-reset-request.command';
import { InvalidateCacheService } from '../../../shared/services/cache';
import { entityBuilder } from '../../../shared/services/cache/keys';
import { buildUserKey } from '../../../shared/services/cache/key-builders/entities';

@Injectable()
export class PasswordResetRequest {
Expand All @@ -27,7 +27,7 @@ export class PasswordResetRequest {
const token = uuidv4();

await this.invalidateCache.invalidateByKey({
key: entityBuilder().user({
key: buildUserKey({
_id: foundUser._id,
}),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PasswordResetCommand } from './password-reset.command';
import { ApiException } from '../../../shared/exceptions/api.exception';
import { AuthService } from '../../services/auth.service';
import { InvalidateCacheService } from '../../../shared/services/cache';
import { entityBuilder } from '../../../shared/services/cache/keys';
import { buildUserKey } from '../../../shared/services/cache/key-builders/entities';

@Injectable()
export class PasswordReset {
Expand All @@ -29,7 +29,7 @@ export class PasswordReset {
const passwordHash = await bcrypt.hash(command.password, 10);

await this.invalidateCache.invalidateByKey({
key: entityBuilder().user({
key: buildUserKey({
_id: user._id,
}),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ChangeEntityTypeEnum } from '@novu/shared';
import { ApplyChange, ApplyChangeCommand } from '../apply-change';
import { PromoteTypeChangeCommand } from '../promote-type-change.command';
import { InvalidateCacheService } from '../../../shared/services/cache';
import { notificationTemplateQueryKeyBuild } from '../../../shared/services/cache/keys';
import { buildNotificationTemplateKey } from '../../../shared/services/cache/key-builders/queries';

@Injectable()
export class PromoteNotificationTemplateChange {
Expand Down Expand Up @@ -137,7 +137,7 @@ export class PromoteNotificationTemplateChange {
}

await this.invalidateCache.invalidateQuery({
key: notificationTemplateQueryKeyBuild().invalidate({
key: buildNotificationTemplateKey().invalidate({
_environmentId: command.environmentId,
}),
});
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/app/events/e2e/process-subscriber.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import axios from 'axios';
import { ChannelTypeEnum, ISubscribersDefine, StepTypeEnum } from '@novu/shared';
import { UpdateSubscriberPreferenceRequestDto } from '../../widgets/dtos/update-subscriber-preference-request.dto';
import { CacheService, InvalidateCacheService } from '../../shared/services/cache';
import { notificationTemplateQueryKeyBuild } from '../../shared/services/cache/keys';
import { buildNotificationTemplateKey } from '../../shared/services/cache/key-builders/queries';

const axiosInstance = axios.create();

Expand Down Expand Up @@ -195,7 +195,7 @@ describe('Trigger event - process subscriber /v1/events/trigger (POST)', functio
await updateSubscriberPreference(updateData, session.subscriberToken, template._id);

await invalidateCache.invalidateQuery({
key: notificationTemplateQueryKeyBuild().invalidate({
key: buildNotificationTemplateKey().invalidate({
_environmentId: session.environment._id,
}),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ParseEventRequestCommand } from './parse-event-request.command';
import { TriggerHandlerQueueService } from '../../services/workflow-queue/trigger-handler-queue.service';
import { MapTriggerRecipients, MapTriggerRecipientsCommand } from '../map-trigger-recipients';
import { CachedQuery } from '../../../shared/interceptors/cached-query.interceptor';
import { notificationTemplateQueryKeyBuild } from '../../../shared/services/cache/keys';
import { buildNotificationTemplateKey } from '../../../shared/services/cache/key-builders/queries';

@Injectable()
export class ParseEventRequest {
Expand Down Expand Up @@ -139,7 +139,7 @@ export class ParseEventRequest {

@CachedQuery({
builder: (command: { triggerIdentifier: string; environmentId: string }) =>
notificationTemplateQueryKeyBuild().cache({
buildNotificationTemplateKey().cache({
_environmentId: command.environmentId,
identifiers: { triggerIdentifier: command.triggerIdentifier },
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { InvalidateCacheService } from '../../../shared/services/cache';
import { SendMessageBase } from './send-message.base';
import { ApiException } from '../../../shared/exceptions/api.exception';
import { GetDecryptedIntegrations } from '../../../integrations/usecases/get-decrypted-integrations';
import { queryBuilder } from '../../../shared/services/cache/keys';
import { buildFeedKey, buildMessageCountKey } from '../../../shared/services/cache/key-builders/queries';

@Injectable()
export class SendMessageInApp extends SendMessageBase {
Expand Down Expand Up @@ -148,14 +148,14 @@ export class SendMessageInApp extends SendMessageBase {
let message: MessageEntity | null = null;

await this.invalidateCache.invalidateQuery({
key: queryBuilder().feed().invalidate({
key: buildFeedKey().invalidate({
subscriberId: subscriber.subscriberId,
_environmentId: command.environmentId,
}),
});

await this.invalidateCache.invalidateQuery({
key: queryBuilder().messageCount().invalidate({
key: buildMessageCountKey().invalidate({
subscriberId: subscriber.subscriberId,
_environmentId: command.environmentId,
}),
Expand Down
18 changes: 6 additions & 12 deletions apps/api/src/app/events/usecases/send-message/send-message.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ import {
} from '../../../execution-details/usecases/create-execution-details';
import { DetailEnum } from '../../../execution-details/types';
import { CachedEntity } from '../../../shared/interceptors/cached-entity.interceptor';
import {
buildCommonKey,
buildQueryKey,
CacheKeyPrefixEnum,
CacheKeyTypeEnum,
} from '../../../shared/services/cache/keys';
import { buildSubscriberKey } from '../../../shared/services/cache/key-builders/entities';
import { buildQueryKey } from '../../../shared/services/cache/key-builders/queries';
import { CacheKeyPrefixEnum, CacheKeyTypeEnum } from '../../../shared/services/cache/key-builders/shared';
import { CachedQuery } from '../../../shared/interceptors/cached-query.interceptor';

export abstract class SendMessageBase extends SendMessageType {
Expand All @@ -35,12 +32,9 @@ export abstract class SendMessageBase extends SendMessageType {

@CachedEntity({
builder: (command: { subscriberId: string; _environmentId: string }) =>
buildCommonKey({
type: CacheKeyTypeEnum.ENTITY,
keyEntity: CacheKeyPrefixEnum.SUBSCRIBER,
environmentId: command._environmentId,
identifier: command.subscriberId,
identifierPrefix: 's',
buildSubscriberKey({
_environmentId: command._environmentId,
subscriberId: command.subscriberId,
}),
})
protected async getSubscriberBySubscriberId({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ import {
import { ANALYTICS_SERVICE } from '../../../shared/shared.module';
import { ApiException } from '../../../shared/exceptions/api.exception';
import { CachedEntity } from '../../../shared/interceptors/cached-entity.interceptor';
import {
buildCommonKey,
CacheKeyPrefixEnum,
CacheKeyTypeEnum,
notificationTemplateQueryKeyBuild,
} from '../../../shared/services/cache/keys';
import { CachedQuery } from '../../../shared/interceptors/cached-query.interceptor';
import { buildNotificationTemplateKey } from '../../../shared/services/cache/key-builders/queries';
import { buildSubscriberKey } from '../../../shared/services/cache/key-builders/entities';

@Injectable()
export class SendMessage {
Expand Down Expand Up @@ -179,12 +175,9 @@ export class SendMessage {

@CachedEntity({
builder: (command: { subscriberId: string; _environmentId: string }) =>
buildCommonKey({
type: CacheKeyTypeEnum.ENTITY,
keyEntity: CacheKeyPrefixEnum.SUBSCRIBER,
environmentId: command._environmentId,
identifier: command.subscriberId,
identifierPrefix: 's',
buildSubscriberKey({
_environmentId: command._environmentId,
subscriberId: command.subscriberId,
}),
})
private async getSubscriberBySubscriberId({
Expand Down Expand Up @@ -241,7 +234,7 @@ export class SendMessage {

@CachedQuery({
builder: (command: { _id: string; environmentId: string }) =>
notificationTemplateQueryKeyBuild().cache({
buildNotificationTemplateKey().cache({
_environmentId: command.environmentId,
identifiers: { id: command._id },
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { ApiException } from '../../../shared/exceptions/api.exception';
const LOG_CONTEXT = 'TriggerEventUseCase';

import { PinoLogger } from '@novu/application-generic';
import { notificationTemplateQueryKeyBuild } from '../../../shared/services/cache/keys';
import { CachedQuery } from '../../../shared/interceptors/cached-query.interceptor';
import { buildNotificationTemplateKey } from '../../../shared/services/cache/key-builders/queries';

@Injectable()
export class TriggerEvent {
Expand Down Expand Up @@ -140,7 +140,7 @@ export class TriggerEvent {

@CachedQuery({
builder: (command: { triggerIdentifier: string; environmentId: string }) =>
notificationTemplateQueryKeyBuild().cache({
buildNotificationTemplateKey().cache({
_environmentId: command.environmentId,
identifiers: { triggerIdentifier: command.triggerIdentifier },
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { DeactivateSimilarChannelIntegrations } from '../deactivate-integration/
import { CheckIntegrationCommand } from '../check-integration/check-integration.command';
import { CheckIntegration } from '../check-integration/check-integration.usecase';
import { ANALYTICS_SERVICE } from '../../../shared/shared.module';
import { CacheKeyPrefixEnum, InvalidateCacheService } from '../../../shared/services/cache';
import { buildCommonKey, CacheKeyTypeEnum } from '../../../shared/services/cache/keys';
import { InvalidateCacheService } from '../../../shared/services/cache';
import { buildIntegrationKey } from '../../../shared/services/cache/key-builders/entities';

@Injectable()
export class CreateIntegration {
Expand Down Expand Up @@ -46,11 +46,9 @@ export class CreateIntegration {
}

await this.invalidateCache.invalidateQuery({
key: buildCommonKey({
type: CacheKeyTypeEnum.QUERY,
keyEntity: CacheKeyPrefixEnum.INTEGRATION,
environmentId: command.environmentId,
identifier: command.userId,
key: buildIntegrationKey({
_environmentId: command.environmentId,
_id: command.userId,
}),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IntegrationRepository, DalException } from '@novu/dal';
import { RemoveIntegrationCommand } from './remove-integration.command';
import { ApiException } from '../../../shared/exceptions/api.exception';
import { InvalidateCacheService } from '../../../shared/services/cache';
import { entityBuilder } from '../../../shared/services/cache/keys';
import { buildIntegrationKey } from '../../../shared/services/cache/key-builders/entities';

@Injectable({
scope: Scope.REQUEST,
Expand All @@ -14,7 +14,7 @@ export class RemoveIntegration {
async execute(command: RemoveIntegrationCommand) {
try {
await this.invalidateCache.invalidateByKey({
key: entityBuilder().integration({
key: buildIntegrationKey({
_id: command.integrationId,
_environmentId: command.environmentId,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { DeactivateSimilarChannelIntegrations } from '../deactivate-integration/
import { AnalyticsService, encryptCredentials } from '@novu/application-generic';
import { CheckIntegration } from '../check-integration/check-integration.usecase';
import { CheckIntegrationCommand } from '../check-integration/check-integration.command';
import { CacheKeyPrefixEnum, InvalidateCacheService } from '../../../shared/services/cache';
import { InvalidateCacheService } from '../../../shared/services/cache';
import { ANALYTICS_SERVICE } from '../../../shared/shared.module';
import { buildCommonKey, CacheKeyTypeEnum } from '../../../shared/services/cache/keys';
import { buildIntegrationKey } from '../../../shared/services/cache/key-builders/entities';

@Injectable()
export class UpdateIntegration {
Expand Down Expand Up @@ -38,11 +38,9 @@ export class UpdateIntegration {
});

await this.invalidateCache.invalidateQuery({
key: buildCommonKey({
type: CacheKeyTypeEnum.QUERY,
keyEntity: CacheKeyPrefixEnum.INTEGRATION,
environmentId: command.environmentId,
identifier: command.userId,
key: buildIntegrationKey({
_environmentId: command.environmentId,
_id: command.userId,
}),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { BadRequestException, Injectable } from '@nestjs/common';
import { MessageEntity, MessageRepository, SubscriberRepository, SubscriberEntity } from '@novu/dal';
import { GetMessagesCommand } from './get-messages.command';
import { CachedEntity } from '../../../shared/interceptors/cached-entity.interceptor';
import { buildCommonKey, CacheKeyPrefixEnum, CacheKeyTypeEnum } from '../../../shared/services/cache/keys';
import { buildSubscriberKey } from '../../../shared/services/cache/key-builders/entities';

@Injectable()
export class GetMessages {
Expand Down Expand Up @@ -52,12 +52,9 @@ export class GetMessages {

@CachedEntity({
builder: (command: { subscriberId: string; _environmentId: string }) =>
buildCommonKey({
type: CacheKeyTypeEnum.ENTITY,
keyEntity: CacheKeyPrefixEnum.SUBSCRIBER,
environmentId: command._environmentId,
identifier: command.subscriberId,
identifierPrefix: 's',
buildSubscriberKey({
_environmentId: command._environmentId,
subscriberId: command.subscriberId,
}),
})
private async fetchSubscriber({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MessageRepository } from '@novu/dal';
import { RemoveMessageCommand } from './remove-message.command';
import { InvalidateCacheService } from '../../../shared/services/cache';
import { ApiException } from '../../../shared/exceptions/api.exception';
import { queryBuilder } from '../../../shared/services/cache/keys';
import { buildFeedKey, buildMessageCountKey } from '../../../shared/services/cache/key-builders/queries';

@Injectable()
export class RemoveMessage {
Expand All @@ -21,14 +21,14 @@ export class RemoveMessage {
if (!message.subscriber) throw new ApiException(`A subscriber was not found for message ${command.messageId}`);

await this.invalidateCache.invalidateQuery({
key: queryBuilder().feed().invalidate({
key: buildFeedKey().invalidate({
subscriberId: message.subscriber.subscriberId,
_environmentId: command.environmentId,
}),
});

await this.invalidateCache.invalidateQuery({
key: queryBuilder().messageCount().invalidate({
key: buildMessageCountKey().invalidate({
subscriberId: message.subscriber.subscriberId,
_environmentId: command.environmentId,
}),
Expand Down
Loading