gpt4 book ai didi

c# - Basic Booksleeve 加 Protobuf-net 加 Lists/SortedSets,实现?

转载 作者:IT王子 更新时间:2023-10-29 06:10:23 41 4
gpt4 key购买 nike

关于将 booksleeve 与 protobuf-net 结合使用,我有一些相当基本的问题。现在我已经实现了一个单例类来管理连接,所以我多次重复使用与 recommended 相同的连接。 .现在我有几个关于组合的实际使用的问题:

  1. “db”整数和“key”字符串的区别/重要性是什么?
  2. 如何使用 protobuf-net 将一堆对象序列化为 SortedSet/List?
  3. 我如何使用 protobuf-net 反序列化来自 SortedSet/List 的一堆对象?

我在想我应该使用 Range() 方法进行检索:

    public IList<T> RetrieveAllAsList()
{
var conn = RedisConnectionManager.Current.GetConnection();
conn.Open();


int length = (int)conn.Lists.GetLength(10, "idk").Result;
byte[][] data = conn.Lists.Range(10, "idk", 0, length-1).Result;

List<T> output = new List<T>();
for (int i = 0; i < data.Length; i++)
{
using (MemoryStream ms = new MemoryStream(data[i]))
{
output.Add(Serializer.Deserialize<T>(ms));
}
}

conn.Close(false);
return output;
}

我将不胜感激在这件事上的任何帮助。谢谢你。

最佳答案

What is the difference/importance of the "db" int and the "key" string?

Redis 允许您将数据分区到多个数据库中。例如,在 stackexchange 网站上,您可以将 stackoverflow.com 相关内容存储在 db=0 中,将 programmers.se.com 相关内容存储在 db=1 中。

在每个数据库中,您都有键 = 值对。键始终是一个字符串。该值可以是以下五种数据类型之一 - String、List、Set、Sorted Set 或 Map。

传统的 Redis 客户端不会强制您提供数据库编号。如果您不提供,则假定 db=0。但是 Booksleeve 要求您提供数据库编号。如果您不在乎,只需将 0 传递给所有 API 调用即可。

不过,字符串键必须是唯一的,并且完全是特定于应用程序的。例如,要存储用户对象,通常的技术是使用 user:1190099 这样的键,将值作为具有键=值对的 Map,例如 {"name":"singlelabs", "id":1190099 ... }

How would I serialize a bunch of objects into a SortedSet/List using protobuf-net? How would I deserialize a bunch of objects from a SortedSet/List using protobuf-net?

首先,您需要决定是否要使用 Protocol Buffer 。

Protocol Buffer 可以将复杂对象序列化/反序列化为二进制 blob。这个 blob 对 Redis 是不透明的。换句话说,您可以设置或获取此二进制 blob,但不能使用任何其他 Redis 功能。

如果你真的想使用 Redis 的列表和排序集,你应该使用 BookSleeve 提供的 API。我没用过 Booksleeve,但是下面两个接口(interface)应该可以解释如何将数据插入到 list/sortedset 中。

http://code.google.com/p/booksleeve/source/browse/BookSleeve/ISortedSetCommands.cs

http://code.google.com/p/booksleeve/source/browse/BookSleeve/IListCommands.cs

关于c# - Basic Booksleeve 加 Protobuf-net 加 Lists/SortedSets,实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10180650/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com