Within our application we use the Axios HttpService to do some request to a third-party api. Because the amount of data returned bij de api is so huge, we would like to cache the responses. In the docs is wasn't able to find some examples of how to do this. I'm currently doing this as follows:
@Module({
imports: [
HttpModule,
CacheModule.register({
ttl: 15,
store: redisStore,
host: 'localhost',
port: 6379,
})
]
})
export class AppModule {}
I register the CacheModule globally. Then import it in the module where i need it.
In the service where i use the third-party api, i create an interceptor where i go and cache the reponses. Very crude and just for testing.
constructor(private readonly httpService: HttpService,
private readonly cache: CacheStore) {
httpService.axiosRef.interceptors.response.use((response) => {
cache.set(response.request.url, response.data);
return response;
}, error => Promise.reject(error));
}
First of all this doesn't run, because the CACHE_MANAGER can't be imported into the CacheModule, for some reason. Second this is a more a Node.js way of creating such interceptors and not the NestJS way. But is this a way to move forward or is there a more effecient way and if yes, what way is that?