In this lesson, we will learn how to store class objects, including object storage and retrieval.
This code demonstrates Memcached storing primitive types (int/bool) and custom Class objects, and highlights a very common pitfall:
Custom entity classes must be marked with the
[Serializable]attribute. Otherwise serialization will fail andnullwill be returned when reading the object.
Wrong Implementation
static void Main(string[] args)
{
MemcachedClient mClient = new MemcachedClient();
// Store and read primitive types (int, bool are natively supported, no serialization attribute required)
mClient.Store(StoreMode.Set, "clicks", 1);
var clicks = mClient.Get<int>("clicks");
mClient.Store(StoreMode.Set, "isOk", true);
var isOk = mClient.Get<bool>("isOk");
// Store custom object
var person = new Person { Id = 1, Name = "Jackson", AddTime = DateTime.Now };
mClient.Store(StoreMode.Set, "person", person);
var p = mClient.Get<Person>("person"); // Will get null here
Console.Read();
}
// 【Wrong】Missing [Serializable]
class Person
{
public int Id { set; get; }
public string Name { set; get; }
public DateTime AddTime { set; get; }
}Code language: JavaScript (javascript)
Observed Behavior
var p = mClient.Get<Person>("person"); returns null.
Cause: EnyimMemcached uses .NET binary serialization by default for object persistence. Classes without the [Serializable] attribute cannot be serialized and cannot be deserialized properly after being cached.
Correct Implementation
Add the serialization attribute to your entity class:
[Serializable] // Mandatory attribute
public class Person
{
public int Id { set; get; }
public string Name { set; get; }
public DateTime AddTime { set; get; }
}Code language: JavaScript (javascript)
Once added, Get<Person>("person") will correctly return the object instance.
Additional Notes
// Generic Get: Recommended usage, automatic type conversion
var p = mClient.Get<Person>("person");
// Non‑generic Get: Returns object, manual cast required afterwards
var p2 = mClient.Get("person");Code language: JavaScript (javascript)
Get<T>(key): Generic method, preferred. Returns type‑specific default value on failure (null for reference types)Get(key): Returns rawobject, you must cast it manually
Full Working Sample
using Enyim.Caching;
using Enyim.Caching.Memcached;
using System;
[Serializable]
public class Person
{
public int Id { set; get; }
public string Name { set; get; }
public DateTime AddTime { set; get; }
}
class Program
{
static void Main(string[] args)
{
MemcachedClient mClient = new MemcachedClient();
mClient.Store(StoreMode.Set, "clicks", 1);
var clicks = mClient.Get<int>("clicks");
mClient.Store(StoreMode.Set, "isOk", true);
var isOk = mClient.Get<bool>("isOk");
var person = new Person { Id = 1, Name = "Jackson", AddTime = DateTime.Now };
mClient.Store(StoreMode.Set, "person", person);
var p = mClient.Get<Person>("person");
Console.WriteLine(p.Name); // Output: Jackson
Console.Read();
}
}Code language: JavaScript (javascript)
Object Persistence