Farrellsoft.Azure.Functions.Extensions.Redis 2.0.0

dotnet add package Farrellsoft.Azure.Functions.Extensions.Redis --version 2.0.0
NuGet\Install-Package Farrellsoft.Azure.Functions.Extensions.Redis -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Farrellsoft.Azure.Functions.Extensions.Redis" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Farrellsoft.Azure.Functions.Extensions.Redis --version 2.0.0
#r "nuget: Farrellsoft.Azure.Functions.Extensions.Redis, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Farrellsoft.Azure.Functions.Extensions.Redis as a Cake Addin
#addin nuget:?package=Farrellsoft.Azure.Functions.Extensions.Redis&version=2.0.0

// Install Farrellsoft.Azure.Functions.Extensions.Redis as a Cake Tool
#tool nuget:?package=Farrellsoft.Azure.Functions.Extensions.Redis&version=2.0.0

Azure Function Redis CSharp Binding

Bind your functions to data in Redis in either a read mode or a write mode, for single values or list of values.

Requirements

Built on .NET 6 (LTS) and leveraging Newtonsoft Json.NET internally. Include the package Farrellsoft.Azure.Functions.Extensions.Redis

Reading Values

Single Value Read

Use the Redis attribute targeting a specific key to read the value out of your Redis instance.

Code Example: Read a value (string or object)
using Farrellsoft.Azure.Functions.Extensions.Redis;
...
public static async Task<IActionResult> Run(
  [Redis(key: "value", Connection = "RedisConnectionString")] string value)
{
}
List Read

Redis lists are supported for either strings or objects represented by JSON strings

Code Example: Read a list of strings (or objects)
using Farrellsoft.Azure.Functions.Extensions.Redis;
...
public static async Task<IActionResult> Run(
  [Redis(key: "values", Connection = "RedisConnectionString")] List<string> values)
{
}
HashMap Read (Key must be a string, Value can be string or object)
using Farrellsoft.Azure.Functions.Extensions.Redis;
...
public static async Task<IActionResult> Run(
  [Redis(key: "values", Connection = "RedisConnectionString")] Dictionary<string, string> valueMap)
{
}

Writing Values

The same attribute can be used to write information back into Redis. In this scenario, a value is required to inform the attribute what the underlying value type in Redis is.

Value Type

The supported values for ValueType are:

  • Single ⇒ will leverage the string related APIs for Redis
  • Collection ⇒ will leverage the List related APIs for Redis

All example will show using ICollector<T>, however, you may also use IAsyncCollector<T>

Code Example: Write a string value to the cache
using Farrellsoft.Azure.Functions.Extensions.Redis;
...
public async Task<IActionResult> Run(
  [Redis(key: "value", valueType: RedisValueType.Single, Connection = "RedisConnectionString")] ICollector<string> values)
{
  values.Add("testvalue")
}

This will overwrite the existing value which is present

Code Example: Write an object to the cache
using Farrellsoft.Azure.Functions.Extensions.Redis;
...
public async Task<IActionResult> Run(
  [Redis(key: "value", valueType: RedisValueType.Single, Connection = "RedisConnectionString")] ICollector<Person> values)
{
  values.Add(new Person { Name = "Foo" });
}

Note: the Person instance shown above will be stored as a string in Redis serialized using Json.NET

Code Example: Write a string to a list of values

The attribute can be used, in conjunction with ValueType.Collection to append values to a Redis list

using Farrellsoft.Azure.Functions.Extensions.Redis;
...
public async Task<IActionResult> Run(
  [Redis(key: "values", valueType: RedisValueType.Collection, Connection = "RedisConnectionString")] ICollector<string> values)
{
  values.Add("test1");
  values.Add("test2");
}
Code Example: Write an object to a list of values

Object values are written as JSON using Serialization through the Json.NET library

using Farrellsoft.Azure.Functions.Extensions.Redis;
...
public async Task<IActionResult> Run(
  [Redis(key: "values", valueType: RedisValueType.Collection, Connection = "RedisConnectionString")] ICollector<Person> values)
{
  values.Add(new Person { Name = "Name 1" });
  values.Add(new Person { Name = "Name 2" });
}
Code Example: Update an existing object in a Redis list

The binding offers the ability to update an existing item in a Redis list via a user generated ID value. To activate this functionality, the POCO being stored must implement the IRedisListItem interface - note in place updating is NOT available for non-objects

namespace Farrellsoft.Azure.Functions.Extensions.Redis
{
	public interface IRedisListItem
	{
		public string Id { get; set; }
	}
}

This Id property must be hydrated with a value that can be used to look up the value in the underlying collection.

namespace Sandbox
{
    public class Person : IRedisListItem
    {
        public string Name { get; set; }
        public string Id { get; set; }
    }
}

Once specified, the binding will fetch all list values for the list and search for the correct item. When found it will update, if it is not found, it will fall back to appending the item, again in JSON

public async Task<IActionResult> Run(
  [Redis(key: "values", valueType: RedisValueType.Collection, Connection = "RedisConnectionString")] ICollector<Person> values)
{
  values.Add(new Person { Id = "1", Name = "Name 1" });
  values.Add(new Person { Id = "1", Name = "Name 2" });
}
Code Example: Retrieve a HashMap from Redis based on a key (v1.2+)

This can be done with either <strong>Dictionary<string, TValue> or Dictionary<string, string>

public async Task<IActionResult> Run(
  [RedisHash(key: "brothers", Connection = "RedisConnectionString")] Dictionary<string, Person> values)
{
  var valueString = string.Join(",", values.Values);

  return new OkObjectResult(valueString);
}

Support

Feel free to contact me on Twitter (@jfarrell) or Mastodon (https://hachyderm.io/@jfarrell) with any questions or feature requests. As time goes on, I intend to create more process around asks.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 250 2/16/2023
1.2.0 259 1/30/2023
1.1.0 274 1/7/2023
1.0.1 273 1/1/2023
1.0.0 253 1/1/2023
0.0.1 264 12/31/2022