We often need to perform certain actions that are expensive in web application development, either because they are computationally heavy, take a long time to complete or require an external API call that is expensive.
Caching is a technique that allows us to store the results of a certain action so we don't have to perform the action again if the same data is requested again.
The lru-cache package can be used to implement an LRU Cache in Node.js with TypeScript.
The LRU Cache has a maximum size of 5 and when we request a sixth user, the least recently used item is removed from the cache to make room for the new data.
When we first request user data, it comes from the API. But when we request the same user again, the data is pulled from the cache, making the request much faster.
If we then request the first user again, it has to be fetched from the API because it's no longer in the cache.
When we request the same user data multiple times, it's served from the cache, making the requests much faster.
This reduces the load on the API, improves application performance, and can save resources and costs.
Overall, using LRU Cache can be very beneficial in web application development to increase performance and save cost.