Azure Functions and Caching

2020-07-01 04:54发布

We are planning to develop an Azure function for which the input trigger is a service bus message and the output will be blob storage. The service bus message will contain a image url and the function will resize the image to a predefined resolution and will upload to azure blob storage.

The resolution to which the image should be resized is stored in the database and the Azure function needs to make a call to database to get to know the resolution that is supposed to be used for the image in the input message. The resolution would actually be a master data configured based on the source of the input message.

Making a database call would be a expensive call as it would have to go to the database for each call. Is there any way to cache the data and use it without calling the database. Like in memory caching?

5条回答
该账号已被封号
2楼-- · 2020-07-01 05:13

Consider something like Cloudfront or Cloudflare. I'm not sure exactly what Microsofts offering is.

This will sit on top of your API and cache requests as well as distribute them around the globe for you.

This can be used in addition to whatever other solutions are presented here but it's got to be one of the easiest fa at eat ways

查看更多
甜甜的少女心
3楼-- · 2020-07-01 05:14

You can use Azure Cache service (https://azure.microsoft.com/en-us/services/cache/) to cache your data. Basically, In your Azure Function instead of calling database all the time, call Azure cache and use if it is not expired and if it is expired or not set then call database to get the value and populate the cache with appropriate expiry logic (timeout after fixed time or some other custom logic).

查看更多
SAY GOODBYE
4楼-- · 2020-07-01 05:24

You are free to use the usual approaches that you would use in other .NET applications:

  • You can cache it in memory. The easiest way is just to declare a static dictionary and put database values inside (use concurrent dictionary if needed). The cached values will be reused for all subsequent Function executions which run on the same instance. If an instance gets idle for 5 minutes, or if App scales out to an extra instance, you will have to read the database again;

  • You can use distributed cache, e.g. Redis, by using its SDK from Function code. Might be a bit nicer, since you keep the stateless nature of Functions, but might cost a bit more. Table Storage is a viable alternative to Redis, but with more limited API.

There's no "caching" feature of Azure Functions themselves, that would be ready to use without any extra code.

查看更多
我只想做你的唯一
5楼-- · 2020-07-01 05:26

Redis is in-memory cache and there is custom output binding that you can use to keep your function clean:

[FunctionName("SetPoco")]
public static async Task<IActionResult> SetPoco(
    [HttpTrigger("POST", Route = "poco/{key}")] HttpRequest request,
    [Redis(Key = "{key}")] IAsyncCollector<CustomObject> collector)
{
    string requestBody;
    using (var reader = new StreamReader(request.Body))
    {
        requestBody = reader.ReadToEnd();
        var value = JsonConvert.DeserializeObject<CustomObject>(requestBody);
        await collector.AddAsync(value);
    }
    return new OkObjectResult(requestBody);
}

Link to the project: https://github.com/daulet/Indigo.Functions#redis

However if by in-memory cache you mean in memory of the function I'd strongly recommend otherwise as function are meant to be stateless and you won't be able to share that memory across multiple hosts running your function. This is also not recommended in Azure Functions best practices

查看更多
神经病院院长
6楼-- · 2020-07-01 05:34

You could use Durable Functions and make the database call via an activity or sub-Orchestration, the return value is essentially cached for you then and will be returned without making the underlying call again each time the function replays.

查看更多
登录 后发表回答