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

Fix cache invalidation #1055

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Changes from all 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
16 changes: 14 additions & 2 deletions src/modules/campaigns/campaigns-caching.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import '../../utils/extensions';
import { Constants } from '@multiversx/sdk-nestjs-common';
import { CacheService } from '@multiversx/sdk-nestjs-cache';
import { CollectionType } from '../assets/models/Collection.type';
import { CacheInfo } from 'src/common/services/caching/entities/cache.info';
import { Campaign } from './models';
import { ClientProxy } from '@nestjs/microservices';

@Injectable()
export class CampaignsCachingService {
constructor(private cacheService: CacheService) {}
constructor(private cacheService: CacheService, @Inject('PUBSUB_SERVICE') private clientProxy: ClientProxy) {}

Check warning on line 12 in src/modules/campaigns/campaigns-caching.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/campaigns/campaigns-caching.service.ts#L12

Added line #L12 was not covered by tests

public async getAllMarketplaces(getCampaignsFromDb: () => any): Promise<CollectionType<Campaign>> {
return await this.cacheService.getOrSet(CacheInfo.Campaigns.key, () => getCampaignsFromDb(), Constants.oneHour());
Expand All @@ -24,5 +25,16 @@

public async invalidateCache() {
await this.cacheService.deleteInCache(CacheInfo.Campaigns.key);
await this.refreshCacheKey(CacheInfo.Campaigns.key, CacheInfo.MarketplaceAddressCollection.ttl);

Check warning on line 28 in src/modules/campaigns/campaigns-caching.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/campaigns/campaigns-caching.service.ts#L28

Added line #L28 was not covered by tests
}

private async refreshCacheKey(key: string, ttl: number) {
this.clientProxy.emit<{

Check warning on line 32 in src/modules/campaigns/campaigns-caching.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/campaigns/campaigns-caching.service.ts#L32

Added line #L32 was not covered by tests
key: string;
ttl: number;
}>('refreshCacheKey', {
key,
ttl,
});
}
}