Skip to content

Commit 70a01f0

Browse files
committed
Merge branch 'main' of https://github.com/MicrosoftDocs/azure-docs-pr into patricka-improve-mqtt-bridge-cmds
2 parents 89b1851 + f17dab4 commit 70a01f0

File tree

35 files changed

+1788
-375
lines changed

35 files changed

+1788
-375
lines changed

articles/azure-functions/TOC.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,15 +751,21 @@
751751
displayName: Azure Cache for Redis
752752
- name: Trigger
753753
items:
754-
- name: Pub Sub Trigger
754+
- name: Pub Sub
755755
href: functions-bindings-cache-trigger-redispubsub.md
756756
displayName: Azure Cache for Redis
757-
- name: List Trigger
757+
- name: List
758758
href: functions-bindings-cache-trigger-redislist.md
759759
displayName: Azure Cache for Redis
760-
- name: Stream Trigger
760+
- name: Stream
761761
href: functions-bindings-cache-trigger-redisstream.md
762762
displayName: Azure Cache for Redis
763+
- name: Input
764+
href: functions-bindings-cache-input.md
765+
displayName: Azure Cache for Redis
766+
- name: Output
767+
href: functions-bindings-cache-output.md
768+
displayName: Azure Cache for Redis
763769
- name: Azure Cosmos DB
764770
items:
765771
- name: Functions 1.x
Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
---
2+
title: Azure Cache for Redis input binding for Azure Functions (preview)
3+
description: Learn how to use input bindings to connect to Azure Cache for Redis from Azure Functions.
4+
author: flang-msft
5+
ms.author: franlanglois
6+
ms.service: azure-functions
7+
ms.custom: devx-track-extended-java, devx-track-js, devx-track-python
8+
ms.topic: reference
9+
ms.date: 02/27/2024
10+
zone_pivot_groups: programming-languages-set-functions-lang-workers
11+
---
12+
13+
# Azure Cache for Redis input binding for Azure Functions (preview)
14+
15+
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.
16+
17+
For information on setup and configuration details, see the [overview](functions-bindings-cache.md).
18+
19+
::: zone pivot="programming-language-javascript"
20+
<!--- Replace with the following when Node.js v4 is supported:
21+
[!INCLUDE [functions-nodejs-model-tabs-description](../../includes/functions-nodejs-model-tabs-description.md)]
22+
-->
23+
[!INCLUDE [functions-nodejs-model-tabs-redis-preview](../../includes/functions-nodejs-model-tabs-redis-preview.md)]
24+
::: zone-end
25+
::: zone pivot="programming-language-python"
26+
<!--- Replace with the following when Python v2 is supported:
27+
[!INCLUDE [functions-python-model-tabs-description](../../includes/functions-python-model-tabs-description.md)]
28+
-->
29+
[!INCLUDE [functions-python-model-tabs-redis-preview](../../includes/functions-python-model-tabs-redis-preview.md)]
30+
::: zone-end
31+
32+
## Example
33+
34+
::: zone pivot="programming-language-csharp"
35+
[!INCLUDE [functions-bindings-csharp-intro](../../includes/functions-bindings-csharp-intro.md)]
36+
37+
> [!IMPORTANT]
38+
>
39+
>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.
40+
41+
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:
42+
43+
### [Isolated process](#tab/isolated-process)
44+
45+
```csharp
46+
using Microsoft.Extensions.Logging;
47+
48+
namespace Microsoft.Azure.Functions.Worker.Extensions.Redis.Samples.RedisInputBinding
49+
{
50+
public class SetGetter
51+
{
52+
private readonly ILogger<SetGetter> logger;
53+
54+
public SetGetter(ILogger<SetGetter> logger)
55+
{
56+
this.logger = logger;
57+
}
58+
59+
[Function(nameof(SetGetter))]
60+
public void Run(
61+
[RedisPubSubTrigger(Common.connectionStringSetting, "__keyevent@0__:set")] string key,
62+
[RedisInput(Common.connectionStringSetting, "GET {Message}")] string value)
63+
{
64+
logger.LogInformation($"Key '{key}' was set to value '{value}'");
65+
}
66+
}
67+
}
68+
69+
```
70+
71+
### [In-process](#tab/in-process)
72+
73+
```csharp
74+
using Microsoft.Extensions.Logging;
75+
76+
namespace Microsoft.Azure.WebJobs.Extensions.Redis.Samples.RedisPubSubTrigger
77+
{
78+
internal class SetGetter
79+
{
80+
[FunctionName(nameof(SetGetter))]
81+
public static void Run(
82+
[RedisPubSubTrigger(Common.connectionStringSetting, "__keyevent@0__:set")] string key,
83+
[Redis(Common.connectionStringSetting, "GET {Message}")] string value,
84+
ILogger logger)
85+
{
86+
logger.LogInformation($"Key '{key}' was set to value '{value}'");
87+
}
88+
}
89+
}
90+
```
91+
92+
---
93+
94+
More samples for the Azure Cache for Redis input binding are available in the [GitHub repository](https://github.com/Azure/azure-functions-redis-extension).
95+
<!-- link to redis samples -->
96+
::: zone-end
97+
::: zone pivot="programming-language-java"
98+
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:
99+
100+
```java
101+
package com.function.RedisInputBinding;
102+
103+
import com.microsoft.azure.functions.*;
104+
import com.microsoft.azure.functions.annotation.*;
105+
import com.microsoft.azure.functions.redis.annotation.*;
106+
107+
public class SetGetter {
108+
@FunctionName("SetGetter")
109+
public void run(
110+
@RedisPubSubTrigger(
111+
name = "key",
112+
connection = "redisConnectionString",
113+
channel = "__keyevent@0__:set")
114+
String key,
115+
@RedisInput(
116+
name = "value",
117+
connection = "redisConnectionString",
118+
command = "GET {Message}")
119+
String value,
120+
final ExecutionContext context) {
121+
context.getLogger().info("Key '" + key + "' was set to value '" + value + "'");
122+
}
123+
}
124+
```
125+
126+
::: zone-end
127+
::: zone pivot="programming-language-javascript"
128+
### [Model v3](#tab/nodejs-v3)
129+
130+
This function.json defines both a pub/sub trigger and an input binding to the GET message on an Azure Cache for Redis instance:
131+
132+
```json
133+
{
134+
"bindings": [
135+
{
136+
"type": "redisPubSubTrigger",
137+
"connection": "redisConnectionString",
138+
"channel": "__keyevent@0__:set",
139+
"name": "key",
140+
"direction": "in"
141+
},
142+
{
143+
"type": "redis",
144+
"connection": "redisConnectionString",
145+
"command": "GET {Message}",
146+
"name": "value",
147+
"direction": "in"
148+
}
149+
],
150+
"scriptFile": "index.js"
151+
}
152+
```
153+
154+
This JavaScript code (from index.js) retrives and logs the cached value related to the key provided by the pub/sub trigger.
155+
156+
```nodejs
157+
158+
module.exports = async function (context, key, value) {
159+
context.log("Key '" + key + "' was set to value '" + value + "'");
160+
}
161+
162+
```
163+
164+
### [Model v4](#tab/nodejs-v4)
165+
166+
<!--- Replace with the following when Node.js v4 is supported:
167+
[!INCLUDE [functions-nodejs-model-tabs-description](../../includes/functions-nodejs-model-tabs-description.md)]
168+
-->
169+
[!INCLUDE [functions-nodejs-model-tabs-redis-preview](../../includes/functions-nodejs-model-tabs-redis-preview.md)]
170+
171+
---
172+
173+
::: zone-end
174+
::: zone pivot="programming-language-powershell"
175+
176+
This function.json defines both a pub/sub trigger and an input binding to the GET message on an Azure Cache for Redis instance:
177+
<!---Note: it might be confusing that the binding `name` and the parameter name are the same in these examples. --->
178+
```json
179+
{
180+
"bindings": [
181+
{
182+
"type": "redisPubSubTrigger",
183+
"connection": "redisConnectionString",
184+
"channel": "__keyevent@0__:set",
185+
"name": "key",
186+
"direction": "in"
187+
},
188+
{
189+
"type": "redis",
190+
"connection": "redisConnectionString",
191+
"command": "GET {Message}",
192+
"name": "value",
193+
"direction": "in"
194+
}
195+
],
196+
"scriptFile": "run.ps1"
197+
}
198+
```
199+
200+
This PowerShell code (from run.ps1) retrieves and logs the cached value related to the key provided by the pub/sub trigger.
201+
202+
```powershell
203+
param($key, $value, $TriggerMetadata)
204+
Write-Host "Key '$key' was set to value '$value'"
205+
```
206+
207+
::: zone-end
208+
::: zone pivot="programming-language-python"
209+
210+
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](functions-reference-python.md).
211+
212+
### [v1](#tab/python-v1)
213+
214+
This function.json defines both a pub/sub trigger and an input binding to the GET message on an Azure Cache for Redis instance:
215+
216+
```json
217+
{
218+
"bindings": [
219+
{
220+
"type": "redisPubSubTrigger",
221+
"connection": "redisConnectionString",
222+
"channel": "__keyevent@0__:set",
223+
"name": "key",
224+
"direction": "in"
225+
},
226+
{
227+
"type": "redis",
228+
"connection": "redisConnectionString",
229+
"command": "GET {Message}",
230+
"name": "value",
231+
"direction": "in"
232+
}
233+
]
234+
}
235+
```
236+
237+
This Python code (from \_\_init\_\_.py) retrives and logs the cached value related to the key provided by the pub/sub trigger:
238+
239+
```python
240+
241+
import logging
242+
243+
def main(key: str, value: str):
244+
logging.info("Key '" + key + "' was set to value '" + value + "'")
245+
246+
```
247+
248+
The [configuration](#configuration) section explains these properties.
249+
250+
### [v2](#tab/python-v2)
251+
252+
<!--- Replace with the following when Python v2 is supported:
253+
[!INCLUDE [functions-python-model-tabs-description](../../includes/functions-python-model-tabs-description.md)]
254+
-->
255+
[!INCLUDE [functions-python-model-tabs-redis-preview](../../includes/functions-python-model-tabs-redis-preview.md)]
256+
---
257+
258+
::: zone-end
259+
::: zone pivot="programming-language-csharp"
260+
261+
## Attributes
262+
263+
> [!NOTE]
264+
> 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](https://github.com/Azure/azure-functions-redis-extension/blob/main/src/Microsoft.Azure.WebJobs.Extensions.Redis/Bindings/RedisAsyncConverter.cs#L63)
265+
266+
|Attribute property | Description |
267+
|-------------------|-----------------------------------|
268+
| `Connection` | The name of the [application setting](functions-how-to-use-azure-function-app-settings.md#settings) that contains the cache connection string, such as: `<cacheName>.redis.cache.windows.net:6380,password...` |
269+
| `Command` | The redis-cli command to be executed on the cache with all arguments separated by spaces, such as: `GET key`, `HGET key field`. |
270+
271+
::: zone-end
272+
::: zone pivot="programming-language-java"
273+
## Annotations
274+
275+
The `RedisInput` annotation supports these properties:
276+
277+
| Property | Description |
278+
|----------|---------------------------------------------------|
279+
| `name` | The name of the specific input binding. |
280+
| `connection` | The name of the [application setting](functions-how-to-use-azure-function-app-settings.md#settings) that contains the cache connection string, such as: `<cacheName>.redis.cache.windows.net:6380,password...` |
281+
| `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`. |
282+
::: zone-end
283+
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
284+
## Configuration
285+
286+
The following table explains the binding configuration properties that you set in the function.json file.
287+
288+
| function.json property | Description |
289+
|------------------------|-------------------------|
290+
| `connection` | The name of the [application setting](functions-how-to-use-azure-function-app-settings.md#settings) that contains the cache connection string, such as: `<cacheName>.redis.cache.windows.net:6380,password...` |
291+
| `command` | The redis-cli command to be executed on the cache with all arguments separated by spaces, such as: `GET key`, `HGET key field`. |
292+
293+
> [!NOTE]
294+
> 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.
295+
296+
::: zone-end
297+
298+
See the [Example section](#example) for complete examples.
299+
300+
## Usage
301+
302+
The input binding expects to receive a string from the cache.
303+
::: zone pivot="programming-language-csharp"
304+
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.
305+
::: zone-end
306+
307+
## Related content
308+
309+
- [Introduction to Azure Functions](functions-overview.md)
310+
- [Tutorial: Get started with Azure Functions triggers in Azure Cache for Redis](/azure/azure-cache-for-redis/cache-tutorial-functions-getting-started)
311+
- [Tutorial: Create a write-behind cache by using Azure Functions and Azure Cache for Redis](/azure/azure-cache-for-redis/cache-tutorial-write-behind)
312+
- [Redis connection string](functions-bindings-cache.md#redis-connection-string)

0 commit comments

Comments
 (0)