Azure函数和缓存 [英] Azure Functions and Caching

查看:97
本文介绍了Azure函数和缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们计划开发一个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:

  • 您可以将其缓存在内存中.最简单的方法是声明一个静态字典并将数据库值放入其中(如果需要,请使用并发字典).缓存的值将重新用于在同一实例上运行的所有后续Function执行.如果某个实例闲置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.可能会更好一些,因为您保留了Function的无状态性质,但可能会花费更多.表存储是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函数和缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆