npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-memoize-endpoint

v1.2.5

Published

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

Downloads

33

Readme

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

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.