In this article I would like to show you how you could use the @memoizeAsync decorator, from the utils-decorators library, in your application (both node and web) in one of the most elegant and simple ways.

Let’s say we have the following class:

export class SettingsProviderClient {
  getSettings(context): Promise<SettingsDto> {
    ...
  }
}

As you can see, the getSettings is returning a Promise of SeettingsDto. Now let’s say that we would like to have some caching which will check if a call was made in the last 10 minutes to getSettings with the same context then it would be pulled from the cache and won’t be resolved again.

First step, install the utils-decorators library:

npm install --save utils-decorators

#typescript #javascript #nodejs

How To Setup Caching in Nodejs
4.70 GEEK