Giter Club home page Giter Club logo

redis.webjobs.extensions's Introduction

Redis.WebJobs.Extensions

Utility extensions for using Redis in Azure WebJobs.

Current implementation borrows heavily from the ServiceBus extensions in the Azure WebJobs SDK. I'm slowly working on refactoring the implementation.

There are two bindings: Redis and RedisTrigger.

#Redis A binding that will publish a message to a Redis Channel.

Example of sending a string message

public static void SendSimpleMessage([Redis("messages", Mode.PubSub)] out string message, TextWriter log)
{
  message = "this is a test";

  log.WriteLine("sending message: " + message);
}

Exmple of sending an object that can be serialized with the Newtonsoft.Json serializer.

public static void SendMessage([Redis("messages", Mode.PubSub)] out Message message, TextWriter log)
{
  message = new Message
  {
    Text = "message #",
    Sent = DateTime.UtcNow,
    Id = Guid.NewGuid()
  };

  log.WriteLine("sending Message: " + message.Id);
}

Example of putting a string into a redis cache:

public static void ReceiveAndStoreSimpleMessage([RedisTrigger("messages", Mode.PubSub)] string message,
  [Redis("LastSimpleMessage", Mode.Cache)] out string lastMessage, TextWriter log)
{
  lastMessage = message;
  log.WriteLine("Last message received: " + message);
  log.WriteLine("Storing with key: LastSimpleMessage");
}

Example of putting an object into a redis cache:

public static void ReceiveAndStoreMessage([RedisTrigger("messages", Mode.PubSub)] Message message,
  [Redis("LastMessage", Mode.Cache)] out Message lastMessage, TextWriter log)
{
  lastMessage = message;
  log.WriteLine("Last message id received: " + message.Id);
  log.WriteLine("Storing with key: LastMessage");
}

Example of retrieving a string value out of a redis cache:

public static void GetSimpleMessage([RedisTrigger("messages", Mode.PubSub)] string message, 
  [Redis("LastSimpleMessage", Mode.Cache)] string lastMessage, TextWriter log)
{
  log.WriteLine("LastSimpleMessage retrieved: " + lastMessage);
}

Example of retrieving an object out of a redis cache:

public static void GetMessage([RedisTrigger("messages", Mode.PubSub)] string message, 
  [Redis("LastMessage", Mode.Cache)] Message lastMessage, TextWriter log)
{
  log.WriteLine("LastMessage retrieved. Id:" + lastMessage.Id + " Text:" + lastMessage.Text);
}

#RedisTrigger This trigger has two modes: PubSub - the trigger will subscribe to a Redis Channel, passing in messages as they are published. Cache - the trigger will check the value of a redis cache key on a determined TimeSpan frequency and compare the value to the previous checked value and will call function if values are different. Uses a string compare on json serialized objects.

Example of receiving string messages

public static void ReceiveSimpleMessage([RedisTrigger("messages", Mode.PubSub)] string message)
{
    Console.WriteLine("Received Message: {0}", message);
}

Example of receiving deserialized json objects in message

public static void ReceiveMessage([RedisTrigger("messages", Mode.PubSub)] Message message, TextWriter log)
{
  if (message != null)
  {
    log.WriteLine("***ReceivedMessage: {0} Sent: {1}", message.Text, message.Sent);
  }
  else
  {
    log.WriteLine("***ReceivedMessage: message sent but not compatible withe Message type");
  }
}

Example of receiving a string messages when cache value for a given key has changed

public static void GetCacheTriggerSimpleMessage([RedisTrigger("LastSimpleMessage", Mode.Cache)] string lastMessage, 
  TextWriter log)
{
  log.WriteLine("LastSimpleMessage retrieved: " + lastMessage);
}

Example of receiving a deserialized json object when a cache value for a given key has changed (uses a string comparison to determine value change)

public static void GetCacheTriggerMessage([RedisTrigger("LastMessage", Mode.Cache)] Message lastMessage, 
  TextWriter log)
{
  log.WriteLine("LastMessage retrieved. Id:" + lastMessage.Id + " Text:" + lastMessage.Text);
}

#NOTE You will need to add your Redis and Azure Storage connection information in the App.config's of the Sample publisher and subscriber if you want to run them locally.

<connectionStrings>
  <add name="AzureWebJobsRedis" connectionString="[your redis name].redis.cache.windows.net,ssl=true,password=[your key],allowAdmin=true,connectTimeout=10000,syncTimeout=10000" />
  <add name="AzureWebJobsDashboard" connectionString="DefaultEndpointsProtocol=https;AccountName=[storage account name];AccountKey=[your storage key]" />
  <add name="AzureWebJobsStorage" connectionString="DefaultEndpointsProtocol=https;AccountName=[storage account name];AccountKey=[your storage key]" />
</connectionStrings>

redis.webjobs.extensions's People

Contributors

jasonhaley avatar

Watchers

Mohammad Forutan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.