I have created an azure function app and created few functions into it and have a requirement to get some cached data used in each azure function. So I want to add something in Function app request pipeline(Like HttpModule in Asp.Net) that will be executing for each request made to function app and will return the cached data.

Any suggestions!

2 Answers

Anass Kartit On Best Solutions

For cached data you can store it locally using memory, or distributed using Redis Cache. You might also want to check Durable functions if you ware trying to build stateful functions. https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-overview

Ravi Sharma On

Use nuget package Microsoft.Extensions.Caching.Memory.

Create a Class like below, inject it to use its save, get method.

public class MemoryCacheService
        private readonly IMemoryCache _cache = new MemoryCache(new MemoryCacheOptions());

        //lives one day
        public void SaveProvider(string provider)
            _cache.Set("provider", provider, new MemoryCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(1) });

        public string GetProvider()
            return _cache.Get("provider") as string;