I’m trying to implement Redis cache along with mongoose in nest.js and I'm looking for a way to check redis cache first before executing find or findOne and return the data from redis otherwise execute query, save result in redis and return the result. The reason I'm not implementing caching as recomended by nest.js is that I'm also using Apollo Server for GraphQL.
@Injectable()
export class MyService {
async getItem(where): Promise<ItemModel> {
const fromCache = await this.cacheService.getValue('itemId');
if(!!fromCache){
return JSON.parse(fromCache);
} else {
const response = await this.ItemModel.find(where);
this.cacheService.setValue('itemId', JSON.stringify(response));
return response
}
}
}
I'd like to move this piece of code to a single place so that I don't have to repeat this code for each query in my code since I have multiple services. I know mongoose middleware has a way to run pre and post functions on queries but I'm just not sure how to accomplish this using.
These are the versions I'm using:
- nestjs v7
- mongoose v5.10.0