Skip to content

Latest commit

 

History

History
328 lines (255 loc) · 11.9 KB

File metadata and controls

328 lines (255 loc) · 11.9 KB
title Azure Cache for Redis input binding for Azure Functions
description Learn how to use input bindings to connect to Azure Cache for Redis from Azure Functions.
author flang-msft
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
zone_pivot_groups programming-languages-set-functions-lang-workers

Azure Cache for Redis input binding for Azure Functions

When a function runs, the Azure Cache for Redis input binding retrieves data from a cache and passes it to your function as an input parameter.

For information on setup and configuration details, see the overview.

Scope of availability for functions bindings

Binding Type Azure Managed Redis Azure Cache for Redis
Input 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.

::: 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" [!INCLUDE functions-bindings-csharp-intro]

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 code uses the key from the pub/sub trigger to obtain and log the value from an input binding using a GET command:

using Microsoft.Extensions.Logging;

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

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

        [Function(nameof(SetGetter))]
        public void Run(
            [RedisPubSubTrigger(Common.connectionStringSetting, "__keyevent@0__:set")] string key,
            [RedisInput(Common.connectionStringSetting, "GET {Message}")] string value)
        {
            logger.LogInformation($"Key '{key}' was set to value '{value}'");
        }
    }
}
using Microsoft.Extensions.Logging;

namespace Microsoft.Azure.WebJobs.Extensions.Redis.Samples.RedisPubSubTrigger
{
    internal class SetGetter
    {
        [FunctionName(nameof(SetGetter))]
        public static void Run(
            [RedisPubSubTrigger(Common.connectionStringSetting, "__keyevent@0__:set")] string key,
            [Redis(Common.connectionStringSetting, "GET {Message}")] string value,
            ILogger logger)
        {
            logger.LogInformation($"Key '{key}' was set to value '{value}'");
        }
    }
}

More samples for the Azure Cache for Redis input binding are available in the GitHub repository.

::: zone-end ::: zone pivot="programming-language-java" The following code uses the key from the pub/sub trigger to obtain and log the value from an input binding using a GET command:

package com.function.RedisInputBinding;

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

public class SetGetter {
    @FunctionName("SetGetter")
    public void run(
            @RedisPubSubTrigger(
                name = "key",
                connection = "redisConnectionString",
                channel = "__keyevent@0__:set")
                String key,
            @RedisInput(
                name = "value",
                connection = "redisConnectionString",
                command = "GET {Message}")
                String value,
            final ExecutionContext context) {
            context.getLogger().info("Key '" + key + "' was set to value '" + value + "'");
    }
}

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

This function.json defines both a pub/sub trigger and an input binding to the GET message on an Azure Cache for Redis instance:

{
    "bindings": [
        {
            "type": "redisPubSubTrigger",
            "connection": "redisConnectionString",
            "channel": "__keyevent@0__:set",
            "name": "key",
            "direction": "in"
        },
        {
            "type": "redis",
            "connection": "redisConnectionString",
            "command": "GET {Message}",
            "name": "value",
            "direction": "in"
        }
    ],
    "scriptFile": "index.js"
}

This JavaScript code (from index.js) retrieves and logs the cached value related to the key provided by the pub/sub trigger.

module.exports = async function (context, key, value) {
    context.log("Key '" + key + "' was set to value '" + value + "'");
}

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


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

This function.json defines both a pub/sub trigger and an input binding to the GET message on an Azure Cache for Redis instance:

{
    "bindings": [
        {
            "type": "redisPubSubTrigger",
            "connection": "redisConnectionString",
            "channel": "__keyevent@0__:set",
            "name": "key",
            "direction": "in"
        },
        {
            "type": "redis",
            "connection": "redisConnectionString",
            "command": "GET {Message}",
            "name": "value",
            "direction": "in"
        }
    ],
    "scriptFile": "run.ps1"
}

This PowerShell code (from run.ps1) retrieves and logs the cached value related to the key provided by the pub/sub trigger.

param($key, $value, $TriggerMetadata)
Write-Host "Key '$key' was set to value '$value'"

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

The following example uses a pub/sub trigger with an input binding to the GET message on an Azure Cache for Redis instance. The example depends on whether you use the v1 or v2 Python programming model.

This function.json defines both a pub/sub trigger and an input binding to the GET message on an Azure Cache for Redis instance:

{
    "bindings": [
        {
            "type": "redisPubSubTrigger",
            "connection": "redisConnectionString",
            "channel": "__keyevent@0__:set",
            "name": "key",
            "direction": "in"
        },
        {
            "type": "redis",
            "connection": "redisConnectionString",
            "command": "GET {Message}",
            "name": "value",
            "direction": "in"
        }
    ]
}

This Python code (from __init__.py) retrieves and logs the cached value related to the key provided by the pub/sub trigger:

import logging

def main(key: str, value: str):
    logging.info("Key '" + key + "' was set to value '" + value + "'")

The configuration section explains these properties.

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

Attributes

Note

Not all commands are supported for this binding. At the moment, only read commands that return a single output are supported. The full list can be found here

Attribute property Description
Connection The name of the application setting that contains the cache connection string, such as: <cacheName>.redis.cache.windows.net:6380,password...
Command The redis-cli command to be executed on the cache with all arguments separated by spaces, such as: GET key, HGET key field.

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

Annotations

The RedisInput annotation supports these properties:

Property Description
name The name of the specific input binding.
connection The name of the application setting that contains the cache connection string, such as: <cacheName>.redis.cache.windows.net:6380,password...
command The redis-cli command to be executed on the cache with all arguments separated by spaces, such as: GET key or HGET key field.

::: 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
connection The name of the application setting that contains the cache connection string, such as: <cacheName>.redis.cache.windows.net:6380,password...
command The redis-cli command to be executed on the cache with all arguments separated by spaces, such as: GET key, HGET key field.

Note

Python v2 and Node.js v4 for Functions don't use function.json to define the function. Both of these new language versions aren't currently supported by Azure Redis Cache bindings.

::: zone-end

See the Example section for complete examples.

Usage

The input binding expects to receive a string from the cache. ::: zone pivot="programming-language-csharp"
When you use a custom type as the binding parameter, the extension tries to deserialize a JSON-formatted string into the custom type of this parameter. ::: zone-end

[!INCLUDE functions-azure-redis-cache-authentication-note]

Related content