问题描述
我们计划开发一个 Azure 函数,其输入触发器是服务总线消息,输出是 blob 存储.服务总线消息将包含一个图像 url,该函数会将图像大小调整为预定义的分辨率,并将上传到 azure blob 存储.
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.
图像应调整大小的分辨率存储在数据库中,Azure 函数需要调用数据库以了解输入消息中图像应使用的分辨率.分辨率实际上是根据输入消息的来源配置的主数据.
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?
推荐答案
您可以自由使用在其他 .NET 应用程序中使用的常用方法:
You are free to use the usual approaches that you would use in other .NET applications:
您可以将其缓存在内存中.最简单的方法就是声明一个静态字典并将数据库值放入其中(如果需要,使用并发字典).缓存的值将被重复用于在同一实例上运行的所有后续函数执行.如果一个实例空闲 5 分钟,或者如果 App 扩展到一个额外的实例,您将不得不再次读取数据库;
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;
您可以使用分布式缓存,例如Redis,通过使用其函数代码中的 SDK.可能会更好一些,因为您保留了 Functions 的无状态特性,但可能会花费更多.表存储是 Redis 的可行替代方案,但 API 更有限.
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.
Azure Functions 本身没有缓存"功能,无需任何额外代码即可使用.
There's no "caching" feature of Azure Functions themselves, that would be ready to use without any extra code.
这篇关于Azure 函数和缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!