Problem Statement –
Azure Functions are stateless in nature. Therefore even though we can use the standard.Net objects to cache values, they don’t persist if the Azure Function scales out or is idle for some time.
In many cases, Azure Functions are used for doing some integrations with other applications. For example, we may have an integration scenario in which we make calls to OAuth Rest API’s. In these cases, we may need to preserve OAuth2 bearer tokens in Azure Function.
Approach – We have some approaches for doing caching in Azure Functions.
a) Using standard Memory Objects – For example, we can create static objects like the dictionary for caching the values.
However as indicated previously, if the function is idle for some time or scales out, the cached value will be lost.
As a side note, below is the code snippet shows how we can implement assembly caching to save values
// Use the mentioned below statement to include required classes
// Static object in which we will save the cache
static readonly ObjectCache tokenCache = MemoryCache.Default;
// Retrieving existing value in the cache
CacheItem tokenContents = tokenCache.GetCacheItem(TokenKey);
if (tokenContents == null)
// Branch when cache doesn’t exist. This would mean we need to regenerate it.
CacheItemPolicy policy = new CacheItemPolicy();
policy.Priority = CacheItemPriority.Default;
// Setting expiration timing for the cache
policy.AbsoluteExpiration = DateTimeOffset.Now.AddHours(1);
tokenContents = new CacheItem(TokenKey, tokenResponse.access_token);
// Branch to retrieve existing value present in the cache
Token = tokenContents.Value.ToString();
b) Using Redis Cache – Its managed by Microsoft is highly scalable and provides super fast access to data. Performance wise it can be good but from the pricing perspective, it can cost more than the other options.
c) Using tables in Storage accounts – For an Azure Storage account, we can create tables and properties. These properties can then be used for saving any information.
Using this approach results in an extra API call to retrieve the value in the Azure storage table but pricing will be less than the Redis Cache.
Mentioned below screenshots show how we can configure an Azure Storage account
- Navigate to Microsoft Azure Storage Explorer and enter the credentials for access in the Azure account. Review that it loads all the available storage accounts
- Click on the Azure Storage account for which we need to add the storage table. Review that it will display the existing tables present in the account
- Right click “Table” and click on “Create Table”. Give a default name of the table. Review that the table gets created with default columns of “Partition Key” and “RowKey”
- To add a new custom property to the Storage Account, click on button “+ Edit”. Review that a new screen to create a custom property pops up.
- Add the property and click save. To add rows in the table, click on button “+Add” and add the rows in the table.
- Make a note of the values present in the column “Partition Key” and “RowKey”. These attributes will be used to retrieve the values saved in azure storage accounts.
Code Snippet to retrieve values saved in the Azure Storage table
Mentioned below blog from Microsoft shows how to retrieve a particular row from storage account table using a key value