分享方式:


Azure Functions 的 Azure Cache for Redis 輸入系結

當函式執行時,Azure Cache for Redis 輸入系結會從快取擷取數據,並將其傳遞至函式做為輸入參數。

如需安裝和組態詳細數據的詳細資訊,請參閱概

重要

Azure Cache for Redis 擴充功能尚不支援適用於 Functions 的 Node.js v4 模型。 如需 v4 模型運作方式的更多詳細資料,請參閱 Azure Functions Node.js 開發人員指南。 若要深入了解 v3 與 v4 之間的差異,請參閱移轉指南

重要

Azure Cache for Redis 擴充功能尚不支援適用於 Functions 的 Python v2 模型。 如需 v2 模型運作方式的更多詳細資料,請參閱 Azure Functions Python 開發人員指南

範例

您可以使用下列其中一種 C# 模式來建立 C# 函式:

  • 隔離的背景工作模型:在與運行時間隔離的背景工作進程中執行的已編譯 C# 函式。 需要隔離的背景工作進程,才能支援在 LTS 和非 LTS 版本 .NET 和 .NET Framework 上執行的 C# 函式。 隔離背景工作進程函式的延伸模組會使用 Microsoft.Azure.Functions.Worker.Extensions.* 命名空間。
  • 同進程模型:在與 Functions 運行時間相同的進程中執行的已編譯 C# 函式。 在此模型的變化中,函式可以使用 C# 腳本來執行,主要支援 C# 入口網站編輯。 進程內函式的延伸模組會使用 Microsoft.Azure.WebJobs.Extensions.* 命名空間。

重要

針對 .NET 函式,建議針對進程內模型使用隔離的背景工作模型。 如需同進程隔離背景工作模型的比較,請參閱隔離的背景工作模型與 Azure Functions 上 .NET 的同進程模型之間的差異

下列程式代碼會使用 pub/sub 觸發程式中的索引鍵,使用 GET 命令從輸入系結取得和記錄值:

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

GitHub 存放庫中提供 Azure Cache for Redis 輸入系結的更多範例。

下列程式代碼會使用 pub/sub 觸發程式中的索引鍵,使用 GET 命令從輸入系結取得和記錄值:

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 + "'");
    }
}

此function.json會定義發佈/子觸發程式和 Azure Cache for Redis 實例上 GET 訊息的輸入系結:

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

這個 JavaScript 程式代碼 (從 index.js) 會重試並記錄與 pub/sub 觸發程式所提供的索引鍵相關的快取值。


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

此function.json會定義發佈/子觸發程式和 Azure Cache for Redis 實例上 GET 訊息的輸入系結:

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

此 PowerShell 程式代碼 (從 run.ps1) 擷取並記錄與 pub/sub 觸發程式所提供的索引鍵相關的快取值。

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

下列範例會使用 pub/sub 觸發程式搭配輸入系結至 Azure Cache for Redis 實例上的 GET 訊息。 此範例取決於您使用的是 v1 或 v2 Python 程式設計模型

此function.json會定義發佈/子觸發程式和 Azure Cache for Redis 實例上 GET 訊息的輸入系結:

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

此 Python 程式代碼 (從 __init__.py) 會重試並記錄與 pub/sub 觸發程式所提供的索引鍵相關的快取值:


import logging

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

區段說明這些屬性。

屬性

注意

並非所有命令都支援此系結。 目前僅支援傳回單一輸出的讀取命令。 您可以在這裡找到 完整清單

屬性內容 描述
Connection 包含快取 連接字串 的應用程式設定名稱,例如:<cacheName>.redis.cache.windows.net:6380,password...
Command 要以空白分隔的所有自變數在快取上執行的 redis-cli 命令,例如: GET keyHGET key field

註釋

RedisInput 解支援下列屬性:

屬性 說明
name 特定輸入系結的名稱。
connection 包含快取 連接字串 的應用程式設定名稱,例如:<cacheName>.redis.cache.windows.net:6380,password...
command 要以空白分隔的所有自變數在快取上執行的 redis-cli 命令,例如: GET keyHGET key field

組態

下表說明您在 function.json 檔案中設定的繫結設定屬性。

function.json 屬性 描述
connection 包含快取 連接字串 的應用程式設定名稱,例如:<cacheName>.redis.cache.windows.net:6380,password...
command 要以空白分隔的所有自變數在快取上執行的 redis-cli 命令,例如: GET keyHGET key field

注意

適用於 Functions 的 Python v2 和 Node.js v4 不會使用 function.json 來定義函式。 Azure Redis 快取系結目前不支援這兩個新語言版本。

如需完整範例,請參閱範例一節。

使用方式

輸入系結預期會從快取接收字串。

當您使用自定義類型做為係結參數時,擴充功能會嘗試將 JSON 格式字串還原串行化為此參數的自定義類型。