适用于 Azure Functions 的 RedisListTrigger

RedisListTrigger 从列表中弹出新元素并将这些条目呈现给函数。

有关Azure托管 Redis 触发器和绑定的详细信息, Azure Functions。

函数触发器的可用性范围

触发器类型 Azure 托管的 Redis Azure Cache for Redis
列表 是 是

重要

使用 Azure 托管 Redis 或 Azure Cache for Redis Enterprise 层时,请使用端口 10000 而不是端口 6380 或 6379。

重要

消耗计划上运行的函数目前不支持 Redis 触发器。

重要

Azure Cache for Redis 缓存扩展尚不支持适用于 Functions 的 Node.js v4 模型。 有关 v4 模型工作原理的更多详细信息,请参阅 Azure Functions Node.js 开发人员指南。 要详细了解 v3 和 v4 之间的差异,请参阅迁移指南。

重要

Azure Cache for Redis 缓存扩展尚不支持适用于 Functions 的 Python v2 模型。 有关 v2 模型工作原理的更多详细信息,请参阅 Azure Functions Python 开发人员指南。

示例

重要

对于 .NET 函数,建议在进程内模型中使用独立辅助角色模型。 有关进程内和独立辅助角色模型的比较,请参阅 Azure Functions 上 .NET 的独立辅助角色模型与进程内模型之间的差异。

以下示例对键 listTest 展开了轮询:

using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.Functions.Worker.Extensions.Redis.Samples.RedisListTrigger
{
    public class SimpleListTrigger
    {
        private readonly ILogger<SimpleListTrigger> logger;

        public SimpleListTrigger(ILogger<SimpleListTrigger> logger)
        {
            this.logger = logger;
        }

        [Function(nameof(SimpleListTrigger))]
        public void Run(
            [RedisListTrigger(Common.connectionStringSetting, "listTest")] string entry)
        {
            logger.LogInformation(entry);
        }
    }
}

以下示例在 listTest 的 localhost Redis 实例中轮询密钥 redisLocalhost:

package com.function.RedisListTrigger;

import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.redis.annotation.*;

public class SimpleListTrigger {
    @FunctionName("SimpleListTrigger")
    public void run(
            @RedisListTrigger(
                name = "req",
                connection = "redisConnectionString",
                key = "listTest",
                pollingIntervalInMs = 1000,
                maxBatchSize = 1)
                String message,
            final ExecutionContext context) {
            context.getLogger().info(message);
    }
}

此示例使用相同的 index.js 文件,function.json 文件中包含绑定数据。

index.js文件如下所示:

module.exports = async function (context, entry) {
    context.log(entry);
}

在function.json中,下面是绑定数据:

{
    "bindings": [
        {
            "type": "redisListTrigger",
            "listPopFromBeginning": true,
            "connection": "redisConnectionString",
            "key": "listTest",
            "pollingIntervalInMs": 1000,
            "maxBatchSize": 16,
            "name": "entry",
            "direction": "in"
        }
      ],
    "scriptFile": "index.js"
}

此示例使用相同的 run.ps1 文件,function.json 文件中包含绑定数据。

run.ps1文件如下所示:

param($entry, $TriggerMetadata)
Write-Host $entry

在function.json中,下面是绑定数据:

{
    "bindings": [
        {
            "type": "redisListTrigger",
            "listPopFromBeginning": true,
            "connection": "redisConnectionString",
            "key": "listTest",
            "pollingIntervalInMs": 1000,
            "maxBatchSize": 16,
            "name": "entry",
            "direction": "in"
        }
      ],
    "scriptFile": "run.ps1"
}

此示例使用相同的 __init__.py 文件,function.json 文件中包含绑定数据。

Python v1 编程模型要求在函数文件夹中的单独 function.json 文件中定义绑定。 有关详细信息,请参阅 Python 开发人员指南。

__init__.py文件如下所示:

import logging

def main(entry: str):
    logging.info(entry)

在function.json中,下面是绑定数据:

{
    "bindings": [
        {
            "type": "redisListTrigger",
            "listPopFromBeginning": true,
            "connection": "redisConnectionString",
            "key": "listTest",
            "pollingIntervalInMs": 1000,
            "maxBatchSize": 16,
            "name": "entry",
            "direction": "in"
        }
      ],
    "scriptFile": "__init__.py"
}

特性

参数 步骤 需要 默认
Connection 包含缓存连接字符串的应用程序设置的名称,例如:<cacheName>.redis.cache.chinacloudapi.cn:6380,password... 是
Key 要从中读取的键。 可以使用 INameResolver 解析此字段。 是
PollingIntervalInMs 轮询 Redis 的频率(以毫秒为单位)。 可选 1000
MessagesPerWorker 每个函数实例应处理的消息数。 用于确定函数应缩放到的实例数。 可选 100
Count 一次从 Redis 中弹出的条目数。 系统会并行处理条目。 仅在使用COUNT和LPOP中的RPOP参数的 Redis 6.2+ 上受支持。 可选 10
ListPopFromBeginning 确定是使用LPOP从头开始弹出条目,还是使用RPOP从末尾弹出条目。 可选 true

批注

参数 步骤 需要 默认
name “条目”
connection 包含缓存连接字符串的应用程序设置的名称,例如:<cacheName>.redis.cache.chinacloudapi.cn:6380,password... 是
key 可以使用 INameResolver 解析此字段。 是
pollingIntervalInMs 轮询 Redis 的频率(以毫秒为单位)。 可选 1000
messagesPerWorker 每个函数实例应处理的消息数。 用于确定函数应缩放到的实例数。 可选 100
count 一次从 Redis 中读取的条目数。 这些是并行处理的。 可选 10
listPopFromBeginning 是否在函数运行后删除流条目。 是 true

配置

下表解释了在 function.json 文件中设置的绑定配置属性。

function.json 属性 步骤 可选 默认
type 触发器的名称。 否
listPopFromBeginning 是否在函数运行后删除流条目。 设置为 true。 是 true
connection 包含缓存连接字符串的应用程序设置的名称,例如:<cacheName>.redis.cache.chinacloudapi.cn:6380,password... 否
key 可以使用 INameResolver 解析此字段。 否
pollingIntervalInMs 轮询 Redis 的频率(以毫秒为单位)。 是 1000
messagesPerWorker 每个函数实例应处理的消息数。 用于确定函数应缩放到的实例数。 是 100
count 一次从缓存中读取的条目数。 系统会并行处理条目。 是 10
name ? 是
direction 设置为 in。 否

有关完整示例,请参阅示例部分。

使用情况

RedisListTrigger 从列表中弹出新元素并将这些条目呈现给函数。 触发器以可配置的固定间隔轮询 Redis,并使用LPOP和RPOP从列表中弹出条目。

类型 步骤
byte[] 来自通道的消息。
string 来自通道的消息。
Custom 触发器使用 Json.NET 序列化将通道中的消息从 string 映射到自定义类型。