Using MessagePack for Serialization
The MessagePack-CSharp library provides a high-performance serializer that is much faster than the default BinaryFormatter used by the Scaleout.Client library. Use the CacheBuilder.SetSerialization method to use MessagePack for serialization.
Example
using System;
using System.IO;
using System.Collections.Generic;
using Scaleout.Client;
using MessagePack;
// Required NuGet package: MessagePack
[MessagePackObject]
public class Player
{
[Key(0)]
public string PlayerId { get; set; }
[Key(1)]
public List<int> ScoreHistory { get; set; }
}
class Program
{
static void Main(string[] args)
{
var conn = GridConnection.Connect("bootstrapGateways=localhost:721");
// Configure cache for MessagePack serialization:
var builder = new CacheBuilder<int, Player>("players", conn);
builder.SetSerialization(SerializePlayer, DeserializePlayer);
var playerCache = builder.Build();
}
public static void SerializePlayer(Player player, Stream stream)
{
MessagePackSerializer.Serialize(stream, player);
}
public static Player DeserializePlayer(Stream stream)
{
return MessagePackSerializer.Deserialize<Player>(stream);
}
}
Using Lambdas
For brevity, lambda expressions can be passed directly into the CacheBuilder.SetSerialization call highlighted above. This eliminates the need for the SerializePlayer
and DeserializePlayer
method definitions:
builder.SetSerialization(
(player, stream) => MessagePackSerializer.Serialize(stream, player),
(stream) => MessagePackSerializer.Deserialize<Player>(stream)
);