-
Couldn't load subscription status.
- Fork 0
Caching Memoization
The dependency injection container is able to cache the methods or functions of each service, which significantly improves the performance of applications that call functions that are expensive to compute. It is also useful for large applications that make multiple calls to methods or functions that perform the same operation over and over again.
The caching system is implemented on a middleware with the lowest priority, allowing the middleware to execute as close to the service execution as possible.

See Pure Functions Considerations for more information about using the caching system with non-pure functions.
To enable the caching for all methods, you can use the cache option when registering the service:
container.add('Service', () => {
return new Service();
}, { cache: true });With the methods option you can specify a list of methods that should be handled by the cache if the cache is enabled.
container.add('Service', () => {
return new Service();
}, {
cache: true,
methods: ['someMethod1', 'someMethod2'],
});This caches only the methods
someMethod1andsomeMethod2of the service.
With the excludeMode option you can specify a list of methods that should not be cached. This is useful if you have
a large number of methods and only want to exclude some of them.
container.add('Service', () => {
return new Service();
}, {
cache: true,
methods: ['someMethod1', 'someMethod2'],
excludeMode: true,
});This caches all the methods of the service except the methods
someMethod1andsomeMethod2.
The default behavior, without the cache option, is to not cache any method of the service.
container.add('Service', () => {
return new Service();
});We welcome contributions from the community! Whether it's reporting a bug, suggesting a feature, or contributing code, your involvement is key to making Packet.js better. Please check out this section for more details.
If you encounter any issues or bugs while using packetjs, or if you have any feature requests, please feel free to
open an issue in our GitHub repository. You can report issues, track progress, and engage in discussions with the
community via the following link:
If you have any questions or need further assistance, feel free to reach out via the discussion board.