From ee87692e711d1c9eb246b20b7a60f5188ff4a8de Mon Sep 17 00:00:00 2001 From: danielailie Date: Mon, 20 Nov 2023 11:07:59 +0200 Subject: [PATCH] Fix cache invalidation --- .../campaigns/campaigns-caching.service.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/modules/campaigns/campaigns-caching.service.ts b/src/modules/campaigns/campaigns-caching.service.ts index f107e7dc4..d3b080ca2 100644 --- a/src/modules/campaigns/campaigns-caching.service.ts +++ b/src/modules/campaigns/campaigns-caching.service.ts @@ -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) {} public async getAllMarketplaces(getCampaignsFromDb: () => any): Promise> { return await this.cacheService.getOrSet(CacheInfo.Campaigns.key, () => getCampaignsFromDb(), Constants.oneHour()); @@ -24,5 +25,16 @@ export class CampaignsCachingService { public async invalidateCache() { await this.cacheService.deleteInCache(CacheInfo.Campaigns.key); + await this.refreshCacheKey(CacheInfo.Campaigns.key, CacheInfo.MarketplaceAddressCollection.ttl); + } + + private async refreshCacheKey(key: string, ttl: number) { + this.clientProxy.emit<{ + key: string; + ttl: number; + }>('refreshCacheKey', { + key, + ttl, + }); } }