Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
nestjs-memoize-endpoint
Advanced tools
Readme
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.
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.
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.
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).
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
}
}
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
}
}
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.
FAQs
A NestJS decorator that memoize endpoint results based on the given parameters, improving performance and response times.
The npm package nestjs-memoize-endpoint receives a total of 25 weekly downloads. As such, nestjs-memoize-endpoint popularity was classified as not popular.
We found that nestjs-memoize-endpoint demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.