| title | Using Redis Output bindings with Azure Functions for Azure Managed Redis |
|---|---|
| description | Learn how to use Redis output binding on an Azure Functions. |
| 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 |
The Azure Managed Redis output bindings lets you change the keys in a cache based on a set of available trigger on the cache.
For information on setup and configuration details, see the overview.
| Binding Type | Azure Managed Redis | Azure Cache for Redis |
|---|---|---|
| Output | 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
::: zone pivot="programming-language-csharp" [!INCLUDE functions-bindings-csharp-intro]
The following example shows a pub/sub trigger on the set event with an output binding to the same Redis instance. The set event triggers the cache and the output binding returns a delete command for the key that triggered the function.
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.
using Microsoft.Extensions.Logging;
namespace Microsoft.Azure.Functions.Worker.Extensions.Redis.Samples.RedisOutputBinding
{
internal class SetDeleter
{
[Function(nameof(SetDeleter))]
[RedisOutput(Common.connectionString, "DEL")]
public static string Run(
[RedisPubSubTrigger(Common.connectionString, "__keyevent@0__:set")] string key,
ILogger logger)
{
logger.LogInformation($"Deleting recently SET key '{key}'");
return key;
}
}
}using Microsoft.Extensions.Logging;
namespace Microsoft.Azure.WebJobs.Extensions.Redis.Samples.RedisOutputBinding
{
internal class SetDeleter
{
[FunctionName(nameof(SetDeleter))]
public static void Run(
[RedisPubSubTrigger(Common.connectionStringSetting, "__keyevent@0__:set")] string key,
[Redis(Common.connectionStringSetting, "DEL")] out string[] arguments,
ILogger logger)
{
logger.LogInformation($"Deleting recently SET key '{key}'");
arguments = new string[] { key };
}
}
}::: zone-end ::: zone pivot="programming-language-java" The following example shows a pub/sub trigger on the set event with an output binding to the same Redis instance. The set event triggers the cache and the output binding returns a delete command for the key that triggered the function.
package com.function.RedisOutputBinding;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.redis.annotation.*;
public class SetDeleter {
@FunctionName("SetDeleter")
@RedisOutput(
name = "value",
connection = "redisConnectionString",
command = "DEL")
public String run(
@RedisPubSubTrigger(
name = "key",
connection = "redisConnectionString",
channel = "__keyevent@0__:set")
String key,
final ExecutionContext context) {
context.getLogger().info("Deleting recently SET key '" + key + "'");
return key;
}
}::: zone-end
::: zone pivot="programming-language-javascript"
This example shows a pub/sub trigger on the set event with an output binding to the same Redis instance. The set event triggers the cache and the output binding returns a delete command for the key that triggered the function.
The bindings are defined in the `function.json`` file:
{
"bindings": [
{
"type": "redisPubSubTrigger",
"connection": "redisConnectionString",
"channel": "__keyevent@0__:set",
"name": "key",
"direction": "in"
},
{
"type": "redis",
"connection": "redisConnectionString",
"command": "DEL",
"name": "$return",
"direction": "out"
}
],
"scriptFile": "index.js"
}This code from the index.js file takes the key from the trigger and returns it to the output binding to delete the cached item.
module.exports = async function (context, key) {
context.log("Deleting recently SET key '" + key + "'");
return key;
}[!INCLUDE functions-nodejs-model-tabs-redis-preview]
::: zone-end
::: zone pivot="programming-language-powershell"
This example shows a pub/sub trigger on the set event with an output binding to the same Redis instance. The set event triggers the cache and the output binding returns a delete command for the key that triggered the function.
The bindings are defined in this function.json file:
{
"bindings": [
{
"type": "redisPubSubTrigger",
"connection": "redisLocalhost",
"channel": "__keyevent@0__:set",
"name": "key",
"direction": "in"
},
{
"type": "redis",
"connection": "redisLocalhost",
"command": "DEL",
"name": "retVal",
"direction": "out"
}
],
"scriptFile": "run.ps1"
}
This code from the run.ps1 file takes the key from the trigger and passes it to the output binding to delete the cached item.
param($key, $TriggerMetadata)
Write-Host "Deleting recently SET key '$key'"
Push-OutputBinding -Name retVal -Value $key::: zone-end
::: zone pivot="programming-language-python"
This example shows a pub/sub trigger on the set event with an output binding to the same Redis instance. The set event triggers the cache and the output binding returns a delete command for the key that triggered the function.
The bindings are defined in this function.json file:
{
"bindings": [
{
"type": "redisPubSubTrigger",
"connection": "redisLocalhost",
"channel": "__keyevent@0__:set",
"name": "key",
"direction": "in"
},
{
"type": "redis",
"connection": "redisLocalhost",
"command": "DEL",
"name": "$return",
"direction": "out"
}
],
"scriptFile": "__init__.py"
}This code from the __init__.py file takes the key from the trigger and passes it to the output binding to delete the cached item.
import logging
def main(key: str) -> str:
logging.info("Deleting recently SET key '" + key + "'")
return key[!INCLUDE functions-nodejs-model-tabs-redis-preview]
::: zone-end
::: zone pivot="programming-language-csharp"
Note
All commands are supported for this binding.
The way in which you define an output binding parameter depends on whether your C# functions runs in-process or in an isolated worker process.
The output binding is defined this way:
| Definition | Example | Description |
|---|---|---|
On an out parameter |
[Redis(<Connection>, <Command>)] out string <Return_Variable> |
The string variable returned by the method is a key value that the binding uses to execute the command against the specific cache. |
In this case, the type returned by the method is a key value that the binding uses to execute the command against the specific cache.
When your function has multiple output bindings, you can instead apply the binding attribute to the property of a type that is a key value, which the binding uses to execute the command against the specific cache. For more information, see Multiple output bindings.
Regardless of the C# process mode, the same properties are supported by the output binding attribute:
| 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, such as: DEL. |
::: zone-end
::: zone pivot="programming-language-java"
The RedisOutput 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, such as: DEL. |
::: zone-end
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
The following table explains the binding configuration properties that you set in the function.json file.
| 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, such as: DEL. |
::: zone-end
See the Example section for complete examples.
The output returns a string, which is the key of the cache entry on which apply the specific command.
There are three types of connections that are allowed from an Azure Functions instance to a Redis Cache in your deployments. For local development, you can also use service principal secrets. Use the appsettings to configure each of the following types of client authentication, assuming the Connection was set to Redis in the function.