Skip to content

Latest commit

 

History

History
331 lines (254 loc) · 15.6 KB

File metadata and controls

331 lines (254 loc) · 15.6 KB
title RedisListTrigger for Azure Functions
description Learn how to use the RedisListTrigger Azure Functions for Azure Managed Redis.
author flang-msft
zone_pivot_groups programming-languages-set-functions-lang-workers
ms.author franlanglois
ms.service azure-functions
ms.custom devx-track-extended-java, devx-track-js, devx-track-python, ignite-2024
ms.topic reference
ms.date 07/12/2024

RedisListTrigger for Azure Functions

The RedisListTrigger pops new elements from a list and surfaces those entries to the function.

For more information about Azure Managed Redis triggers and bindings, Redis Extension for Azure Functions.

Scope of availability for functions triggers

Trigger Type Azure Managed Redis Azure Cache for Redis
Lists Yes Yes

Important

When using Azure Managed Redis or the Enterprise tiers of Azure Cache for Redis, use port 10000 rather than port 6380 or 6379.

Important

Redis triggers aren't currently supported for functions running on a Consumption plan or a Flex Consumption plan.

::: zone pivot="programming-language-javascript"

[!INCLUDE functions-nodejs-model-tabs-redis-preview]
::: zone-end
::: zone pivot="programming-language-python"

[!INCLUDE functions-python-model-tabs-redis-preview] ::: zone-end

Example

::: zone pivot="programming-language-csharp"

Important

For .NET functions, using the isolated worker model is recommended over the in-process model. For a comparison of the in-process and isolated worker models, see differences between the isolated worker model and the in-process model for .NET on Azure Functions.

The following sample polls the key 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);
        }
    }
}

[!INCLUDE functions-in-process-model-retirement-note]

using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Extensions.Redis.Samples.RedisListTrigger
{
    internal class SimpleListTrigger
    {
        [FunctionName(nameof(SimpleListTrigger))]
        public static void Run(
            [RedisListTrigger(Common.connectionStringSetting, "listTest")] string entry,
            ILogger logger)
        {
            logger.LogInformation(entry);
        }
    }
}

::: zone-end ::: zone pivot="programming-language-java"

The following sample polls the key listTest at a localhost Redis instance at 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);
    }
}

::: zone-end ::: zone pivot="programming-language-javascript"

This sample uses the same index.js file, with binding data in the function.json file.

Here's the index.js file:

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

From function.json, here's the binding data:

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

[!INCLUDE functions-nodejs-model-tabs-redis-preview]


::: zone-end ::: zone pivot="programming-language-powershell"

This sample uses the same run.ps1 file, with binding data in the function.json file.

Here's the run.ps1 file:

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

From function.json, here's the binding data:

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

::: zone-end ::: zone pivot="programming-language-python"

This sample uses the same __init__.py file, with binding data in the function.json file.

The Python v1 programming model requires you to define bindings in a separate function.json file in the function folder. For more information, see the Python developer guide.

Here's the __init__.py file:

import logging

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

From function.json, here's the binding data:

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

[!INCLUDE functions-python-model-tabs-redis-preview]


::: zone-end ::: zone pivot="programming-language-csharp"

Attributes

Parameter Description Required Default
Connection The name of the application setting that contains the cache connection string, such as: <cacheName>.redis.cache.windows.net:6380,password... Yes
Key Key to read from. This field can be resolved using INameResolver. Yes
PollingIntervalInMs How often to poll Redis in milliseconds. Optional 1000
MessagesPerWorker How many messages each functions instance should process. Used to determine how many instances the function should scale to. Optional 100
Count Number of entries to pop from Redis at one time. Entries are processed in parallel. Only supported on Redis 6.2+ using the COUNT argument in LPOP and RPOP. Optional 10
ListPopFromBeginning Determines whether to pop entries from the beginning using LPOP, or to pop entries from the end using RPOP. Optional true

::: zone-end ::: zone pivot="programming-language-java"

Annotations

Parameter Description Required Default
name "entry"
connection The name of the application setting that contains the cache connection string, such as: <cacheName>.redis.cache.windows.net:6380,password... Yes
key This field can be resolved using INameResolver. Yes
pollingIntervalInMs How often to poll Redis in milliseconds. Optional 1000
messagesPerWorker How many messages each functions instance should process. Used to determine how many instances the function should scale to. Optional 100
count Number of entries to read from Redis at one time. These are processed in parallel. Optional 10
listPopFromBeginning Whether to delete the stream entries after the function has run. Yes true

::: zone-end ::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"

Configuration

The following table explains the binding configuration properties that you set in the function.json file.

function.json Property Description Optional Default
type Name of the trigger. No
listPopFromBeginning Whether to delete the stream entries after the function has run. Set to true. Yes true
connection The name of the application setting that contains the cache connection string, such as: <cacheName>.redis.cache.windows.net:6380,password... No
key This field can be resolved using INameResolver. No
pollingIntervalInMs How often to poll Redis in milliseconds. Yes 1000
messagesPerWorker How many messages each functions instance should process. Used to determine how many instances the function should scale to. Yes 100
count Number of entries to read from the cache at one time. Entries are processed in parallel. Yes 10
name ? Yes
direction Set to in. No

::: zone-end

See the Example section for complete examples.

Usage

The RedisListTrigger pops new elements from a list and surfaces those entries to the function. The trigger polls Redis at a configurable fixed interval, and uses LPOP and RPOP to pop entries from the lists.

::: zone pivot="programming-language-csharp,programming-language-java,programming-language-javascript,programming-language-powershell,programming-language-python"

Type Description
byte[] The message from the channel.
string The message from the channel.
Custom The trigger uses Json.NET serialization to map the message from the channel from a string into a custom type.

::: zone-end

Related content