Azure Cache for Redis output binding for Azure Functions
The Azure Cache for 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.
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
A C# function can be created using one of the following C# modes:
- In-process class library: compiled C# function that runs in the same process as the Functions runtime.
- Isolated process class library: compiled C# function that runs in a process isolated from the runtime. Isolated process is required to support C# functions running on .NET 5.0.
- C# script: used primarily when creating C# functions in the Azure portal.
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 };
}
}
}
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;
}
}
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": "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;
}
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
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
Attributes
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.chinacloudapi.cn:6380,password... |
Command |
The redis-cli command to be executed on the cache, such as: DEL . |
Annotations
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.chinacloudapi.cn:6380,password... |
command |
The redis-cli command to be executed on the cache, such as: DEL . |
Configuration
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.chinacloudapi.cn:6380,password... |
command |
The redis-cli command to be executed on the cache, such as: DEL . |
See the Example section for complete examples.
Usage
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.