Azure Functions do not have an out-of-the-box caching mechanism. For caching, the recommended way would be to use a distributed cache like Azure Cache or Redis. These services each come with their price tag and might be too robust for the cache you want to create or need.
For Azure Function created with C#, you can use the good old memory cache. When your function is already warmed up, and processed data once, you can return results much quicker.
ImportantMemory cache is per instance, so when running on a consumption plan, and when it scales to multiple instances, the memory cache needs to rebuild itself. If you run on a massive scale, it’s better to use a distributed caching system.
But what with Node.js?
The simplest option for Node.js JavaScript/TypeScript function is to use a library called node-cache.
The library provides a simple caching module that allows you to get
, set
, and more. It works very similarly to what you can do with memory cache.
Example:
|
|
The code snippet is just a simple example, but this can be very useful. For example, when you want to fetch data and pass this to the function caller. Instead of calling the API each time the function gets invoked, you can now return cached data. It also allows you to specify on a global level or local/key level what the expiration time is. This expiration time is handy to keep your data fresh, but still do not have too many calls.