Socket
Socket
Sign inDemoInstall

nestjs-memoize-endpoint

Package Overview
Dependencies
0
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    nestjs-memoize-endpoint

A NestJS decorator that memoize endpoint results based on the given parameters, improving performance and response times.


Version published
Weekly downloads
30
increased by50%
Maintainers
1
Install size
6.67 kB
Created
Weekly downloads
 

Readme

Source

NestJS Memoize Endpoint

NestJS Memoize is a simple package that provides a decorator for caching the results of your NestJS endpoints. This can help reduce response time and server load when dealing with slow APIs or databases. The decorator, Memoize, allows you to specify how long the results should be stored in the cache.

Table of Contents

  • Installation
  • Usage
  • How It Works

Installation

To install the package, open your terminal or command prompt, navigate to your NestJS project folder, and run the following command:

npm install nestjs-memoize-endpoint

This command installs the nestjs-memoize-endpoint package into your project, making it available for use.

Usage

To use the Memoize decorator in your NestJS project, you'll need to import it and then apply it to the endpoint methods you'd like to cache.

Basic Example

Here's a simple example showing how to use the Memoize decorator:

import { Memoize } from 'nestjs-memoize-endpoint';

class MyController {
  @Memoize()
  async getData(): Promise<any> {
    // Fetch data from a slow API or database
  }
}

In this example, we imported the Memoize decorator from the nestjs-memoize-endpoint package and applied it to the getData method. By default, the decorator will cache the results for 5 minutes (300,000 milliseconds).

Custom Cache Duration

You can also set a custom cache duration by providing the ttl parameter (time-to-live) in milliseconds. The following example sets the cache duration to 1 minute (60,000 milliseconds):

import { Memoize } from 'nestjs-memoize-endpoint';

class MyController {
  @Memoize(60000) // Cache the result for 1 minute
  async getData(): Promise<any> {
    // Fetch data from a slow API or database
  }
}

Handling Multiple Parameters

The Memoize decorator can handle multiple parameters in your endpoint methods. It creates a cache key based on the input parameters, ensuring that each unique combination of parameters gets its own cache entry.

Here's an example with multiple parameters:

import { Memoize } from 'nestjs-memoize-endpoint';

class MyController {
  @Memoize(60000) // Cache the result for 1 minute
  async getData( param1: string, param2: number ): Promise<any> {
    // Fetch data from a slow API or database based on the input parameters
  }
}

How It Works

When you use the Memoize decorator, it checks if there is a cached result for the given method and input parameters. If a cached result exists and it's still within the specified cache duration (the ttl value), the decorator returns the cached result instead of calling the original method.

This helps reduce the time it takes to respond to requests and can also help reduce the load on your server by avoiding repetitive calls to slow APIs or databases.

Remember to use the Memoize decorator responsibly, as caching can lead to outdated information being returned if the data changes frequently. Use it for endpoints where the data doesn't change often, and the performance benefits outweigh the risk of returning slightly outdated information.

Keywords

FAQs

Last updated on 10 May 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc