paint-brush
How to Implement Caching Efficiently in NestJS Using Redisby@airscholar
12,521 reads
12,521 reads

How to Implement Caching Efficiently in NestJS Using Redis

by Yusuf GaniyuJuly 24th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this post, we’ll look at using Redis to give cache capabilities to a NestJS project. Redis is an open-source in-memory data structure store. By lowering the workload required to produce the content, caching can dramatically enhance application speed and scalability. It's simple to distribute the Redis Cache between different servers that our server application runs on. It is simple to add a Todo controller, service, dto and entity to our NestJS application.
featured image - How to Implement Caching Efficiently in NestJS Using Redis
Yusuf Ganiyu HackerNoon profile picture

In particular, caching aids in accelerating an application’s performance, greatly enhancing its effectiveness.


In this post, we’ll look at using Redis to give cache capabilities to a NestJS project. We’ll discuss Redis, what caching is, and the implementation procedure.


What is caching?

A cache is a temporary, often accessed repository of duplicate data in computing. To reduce latency, the data is stored in a place that is simple to get to.


And Redis…

As a database cache, Redis is an open-source in-memory data structure store. It supports a variety of data structures, including lists, sets, sorted sets with range queries, hashes, and more.


By lowering the workload required to produce the content, caching can dramatically enhance application speed and scalability. It is simple to distribute the Redis Cache between different servers that our server application runs on.


Enough of the intros… Let’s dive into NestJS Caching with Redis! :)


Setting up a NestJS App

Let’s create a simple NestJs Application.


$ npm i -g @nestjs/cli
$ nest new nest-cache


Now that our app is created, let’s add a Todo module!


$ nest g resource todo


At this point, a todo controller, service, dto, and entity would be added. But who cares? That’s not the focus of this article! 😊


Here’s what you have been waiting for, Caching!

In this article, we’ll be using cache-manager. Shout out to Bryan Donovan for this awesome package!


Cache-Manager is a cache module for nodejs that allows easy wrapping of functions in cache, tiered caches, and a consistent interface. — Bryan Donovan


To install Cache-manager, run:


$ npm install cache-manager cache-manager-redis-store


As a bonus, we will be adding the ConfigModule to extract .env constants from the file.


$ npm install @nestjs/config


I will be adding a new module redis-cache that will handle all Redis-abstracted operations (this is not mandatory, just preference).


Create a new folder redis-cache in the source folder with redis-cache.service.ts and redis-cache.module.ts.


https://gist.github.com/airscholar/4238947490a3dfd09fb05f5fda2b06cc


At this point, you should have a file structure similar to this:


Add the following piece of code to yourtodo.module.ts and inject the Redis-cache module into todo.service.ts


https://gist.github.com/airscholar/966fe1299beb7896e948bd1e325b2a79


That’s pretty much it and yes, you made it, our favorite cache guru :)!


Start the application.


$ npm start


The full source code is available here.


Thank you for reading 🙏🏻.


If you would like to read about NestJS, DynamoDB and Serverless, check out this article.



Also published here.