VS Code で、Common.cs という名前のファイルをプロジェクトに追加します。 このクラスは、PubSubTrigger に対する JSON でシリアル化された応答を解析するために使われます。
次のコードをコピーして、Common.cs ファイルに貼り付けます。
public class Common
{
public const string connectionString = "redisConnectionString";
public class ChannelMessage
{
public string SubscriptionChannel { get; set; }
public string Channel { get; set; }
public string Message { get; set; }
}
}
RedisTriggers.cs という名前のファイルをプロジェクトに追加します。
次のコード サンプルをコピーして新しいファイルに貼り付けます。
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Extensions.Redis;
public class RedisTriggers
{
private readonly ILogger<RedisTriggers> logger;
public RedisTriggers(ILogger<RedisTriggers> logger)
{
this.logger = logger;
}
// PubSubTrigger function listens to messages from the 'pubsubTest' channel.
[Function("PubSubTrigger")]
public void PubSub(
[RedisPubSubTrigger(Common.connectionString, "pubsubTest")] Common.ChannelMessage channelMessage)
{
logger.LogInformation($"Function triggered on pub/sub message '{channelMessage.Message}' from channel '{channelMessage.Channel}'.");
}
// KeyeventTrigger function listens to key events from the 'del' operation.
[Function("KeyeventTrigger")]
public void Keyevent(
[RedisPubSubTrigger(Common.connectionString, "__keyevent@0__:del")] Common.ChannelMessage channelMessage)
{
logger.LogInformation($"Key '{channelMessage.Message}' deleted.");
}
// KeyspaceTrigger function listens to key events on the 'keyspaceTest' key.
[Function("KeyspaceTrigger")]
public void Keyspace(
[RedisPubSubTrigger(Common.connectionString, "__keyspace@0__:keyspaceTest")] Common.ChannelMessage channelMessage)
{
logger.LogInformation($"Key 'keyspaceTest' was updated with operation '{channelMessage.Message}'");
}
// ListTrigger function listens to changes to the 'listTest' list.
[Function("ListTrigger")]
public void List(
[RedisListTrigger(Common.connectionString, "listTest")] string response)
{
logger.LogInformation(response);
}
// StreamTrigger function listens to changes to the 'streamTest' stream.
[Function("StreamTrigger")]
public void Stream(
[RedisStreamTrigger(Common.connectionString, "streamTest")] string response)
{
logger.LogInformation(response);
}
}
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Extensions.Redis;
public class RedisBindings
{
private readonly ILogger<RedisBindings> logger;
public RedisBindings(ILogger<RedisBindings> logger)
{
this.logger = logger;
}
//This example uses the PubSub trigger to listen to key events on the 'set' operation. A Redis Input binding is used to get the value of the key being set.
[Function("SetGetter")]
public void SetGetter(
[RedisPubSubTrigger(Common.connectionString, "__keyevent@0__:set")] Common.ChannelMessage channelMessage,
[RedisInput(Common.connectionString, "GET {Message}")] string value)
{
logger.LogInformation($"Key '{channelMessage.Message}' was set to value '{value}'");
}
//This example uses the PubSub trigger to listen to key events to the key 'key1'. When key1 is modified, a Redis Output binding is used to set the value of the 'key1modified' key to 'true'.
[Function("SetSetter")]
[RedisOutput(Common.connectionString, "SET")]
public string SetSetter(
[RedisPubSubTrigger(Common.connectionString, "__keyspace@0__:key1")] Common.ChannelMessage channelMessage)
{
logger.LogInformation($"Key '{channelMessage.Message}' was updated. Setting the value of 'key1modified' to 'true'");
return $"key1modified true";
}
}
VS Code の [実行とデバッグ] タブに切り替え、緑色の矢印を選択してコードをローカルでデバッグします。 コードが正常にビルドされるはずです。 ターミナル出力でその進行状況を追跡できます。
入力バインド機能をテストするには、たとえば SET hello world コマンドを使って、キーに新しい値を設定してみます。SetGetter 関数がトリガーされて、更新された値が返されることがわかるはずです。