Table of Contents

Using protobuf-net for Serialization

The protobuf-net 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 protobuf-net as your serializer.

Example

using System;
using System.IO;
using System.Collections.Generic;
using Scaleout.Client;
using ProtoBuf;

[ProtoContract]
public class Player
{
    [ProtoMember(1)]
    public string PlayerId { get; set; }

    [ProtoMember(2)]
    public List<int> ScoreHistory { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var conn = GridConnection.Connect("bootstrapGateways=localhost:721");

        // Configure cache for Protobuf-net 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)
    {
        ProtoBuf.Serializer.Serialize(stream, player);
    }

    public static Player DeserializePlayer(Stream stream)
    {
        return  ProtoBuf.Serializer.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) => ProtoBuf.Serializer.Serialize(stream, player),
        (stream) => ProtoBuf.Serializer.Deserialize<Player>(stream)
);