Muokkaa

Jaa


RedisListTrigger for Azure Functions

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

For more information about Azure Cache for 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 in the Consumption plan.

Important

The Node.js v4 model for Functions isn't yet supported by the Azure Cache for Redis extension. For more details about how the v4 model works, refer to the Azure Functions Node.js developer guide. To learn more about the differences between v3 and v4, refer to the migration guide.

Important

The Python v2 model for Functions isn't yet supported by the Azure Cache for Redis extension. For more details about how the v2 model works, refer to the Azure Functions Python developer guide.

Example

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);
        }
    }
}

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);
    }
}

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"
}

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"
}

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"
}

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

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

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

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.

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.