This site hosts historical documentation. Visit www.terracotta.org for recent product information.
Data that will be shared across languages needs to be serialized. The choice of which serializer to use depends upon your environment. Some planning and consideration needs to go into the choice of a serializer, as there are pros and cons related to each. For example, with a BSON serializer, you can manually annotate the domain object for desired attributes. On the other hand, a Google Protocol Buffer serializer can automatically generate the domain object with the annotation for the serializer for you, however, if you have an existing domain object, then using the automatic generation can be intrusive.
Your serializer/deserializer class should include the following:
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Terracotta.Ehcache;
using Terracotta.Ehcache.Thrift;
public Terracotta.Ehcache.Thrift.Value serializeKey(string key)
{
return ValueHelper.newValue(key);
}
public string deserializeKey(Terracotta.Ehcache. Thrift.Value serializedKey)
{
return (string)ValueHelper.findSetValue (serializedKey);
}
public StoredValue serializeValue(Recipe2 objectValue)
{
MemoryStream ms = new MemoryStream();
// serialize recipe to BSON
BsonWriter writer = new BsonWriter(ms);
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(writer, objectValue);
byte[] barray = ms.ToArray();
Dictionary<String, Object> nvPairs = new Dictionary<String, Object>();
// two indexable attributes
nvPairs["name"] = objectValue.Name;
nvPairs["bakingTime"] = objectValue.BakingTime;
// ship the bson version of the recipe as a byte array
return ValueHelper.nullSafeStoredValue(barray, nvPairs);
}
public Recipe2 deserializeValue (Terracotta.Ehcache.Thrift.StoredValue value)
{
MemoryStream ms = new MemoryStream(value.Value. BinaryValue);
ms.Seek(0, SeekOrigin.Begin);
BsonReader reader = new BsonReader(ms);
JsonSerializer serializer = new JsonSerializer();
Recipe2 recipe = serializer.Deserialize <Recipe2>(reader);
return recipe;
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Terracotta.Ehcache;
using Terracotta.Ehcache.Thrift;
public Value serializeKey(string key)
{
Value val = new Value();
val.StringValue = key;
return val;
}
public string deserializeKey(Value serializedKey)
{
return serializedKey.StringValue;
}
public StoredValue serializeValue(RecipeProject.RecipeStructure objectValue)
{
Dictionary<String, Object> nvPairs = new
Dictionary<String, Object>();
nvPairs.Add(KEY_NAME, objectValue.name);
MemoryStream ms = new MemoryStream();
ProtoBuf.Serializer.Serialize<RecipeProject.
RecipeStructure>(ms,objectValue);
return ValueHelper.nullSafeStoredValue(ms.ToArray(), nvPairs);
}
public RecipeProject.RecipeStructure deserializeValue (StoredValue value)
{
MemoryStream ms = new MemoryStream(value.Value.BinaryValue);
return ProtoBuf.Serializer.Deserialize<RecipeProject. RecipeStructure>(ms);
}
For single-language use cases, it is not necessary to create a serializer/deserializer class. Leverage .NET's native binary serialization by including the provided CSharpNativeSerializer
class in cache operations. For example:
ICachemanager manager = XPlatform.CreateCacheManager(...);
ICache cache<string,string> = manage.GetCache("cachename", new CSharpNativeSerializer<string,string>())
API documentation for the CSharpNativeSerializer
is available in the /apis/csharp/apidoc/
directory of the kit. For more information about .NET native serialization, refer to http://msdn.microsoft.com/en-us/library/7ay27kt9%28v=vs.90%29.aspx.