您在这里有几个选择。
1) 您可以通过使用高级Redis API让ServiceStack为您解决此问题。
public class Poco{ public int Id { get; set; } public string Name { get; set; } public string Description { get; set; }}...// Clientvar client = new RedisClient("localhost", 6379);// This will store the object for you in a Redis hash.client.StoreAsHash(new Poco { Id = 1, Name = "Test Name", Description = "Test Description" });// This will fetch it back for you.var result = client.GetFromHash<Poco>(1);这种方法将使您不必直接处理散列细节。ServiceStack会为您解决所有问题,并将您发送的对象自动填充到哈希中。如果要更新该对象,只需向其发送一个具有相同ID的新对象即可。
不利的一面是,您将放弃对如何将数据存储在Redis中的控制,从而获得更轻松的编程体验。
2) 您自己处理所有东西。没有预先构建的 SetAllEntriesToHash 函数。
// Clientvar client = new RedisClient("localhost", 6379);// Clear all existing keysvar keysToClear = new Dictionary<string,string>();client.GetHashKeys("xxxxx").ForEach(k => keysToClear.Add(k, ""));client.SetRangeInHash("xxxxx", keysToClear);// Save new key/values. client.SetRangeInHash("xxxxx", new List<KeyValuePair<string, string>>{ new KeyValuePair<string, string>("1", "value 1"), new KeyValuePair<string, string>("2", "value 2"), new KeyValuePair<string, string>("3", "value 3"),});或者,仅删除并重新创建哈希值可能会更容易。
我还要提醒您注意 RedisNativeClient
。它允许您运行直接映射到http://redis.io/commands的 Redis命令。



