// Simple put of integral data types into the cache
cache.StringSet("key1", "value");
cache.StringSet("key2", 25);
// Simple get of data types from the cache
string key1 = cache.StringGet("key1");
int key2 = (int)cache.StringGet("key2");
Azure Redis Cache可以使用 .NET 对象和基本数据类型,但 .NET 对象只有在经过序列化之后才能进行缓存。这属于应用程序开发人员的职责。这样,开发人员便可以灵活选择序列化程序。在下列示例中,缓存对象之前,使用了 StackExchange.Redis.IDatabase 类型的扩展类和 BinaryFormatter 来简化这些对象的序列化。
public static class SampleStackExchangeRedisExtensions
{
public static T Get(this IDatabase cache, string key)
{
return Deserialize(cache.StringGet(key));
}
public static object Get(this IDatabase cache, string key)
{
return Deserialize(cache.StringGet(key));
}
public static void Set(this IDatabase cache, string key, object value)
{
cache.StringSet(key, Serialize(value));
}
static byte[] Serialize(object o)
{
if(o == null)
{
return null;
}
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream())
{
binaryFormatter.Serialize(memoryStream, o);
byte[] objectDataAsStream = memoryStream.ToArray();
return objectDataAsStream;
}
}
static T Deserialize(byte[] stream)
{
if(stream == null)
{
return default(T);
}
BinaryFormatter binaryFormatter = new BinaryFormatter();
using (MemoryStream memoryStream = new MemoryStream(stream))
{
T result = (T)binaryFormatter.Deserialize(memoryStream);
return result;
}
}
}
RedisValue 类型可以直接使用字节数组,因此,调用 Get 帮助程序方法时,它会将对象序列化为字节流,然后再缓存该对象。检索项目时,项目会重新序列化为对象,然后返回给调用程序。
ASP.NET 会话状态的应用程序
MVC movie app with Azure Redis Cache in 15 minutes
http://wacel.codeplex.com/
Redis到底该如何利用?
Redis缓存,Azure灾难恢复,标签,SQLDB弹性比例,文档数据库
Redis编程实践【pub/sub】
Spring mvc Data Redis—Pub/Sub(附Web项目源码)
https://github.com/cargomedia/socket-redis