c# - ServiceStack Redis在检索数据时是如何发挥作用的

标签 c# redis servicestack

不确定这是否是问题的最佳标题...也许有人可以为我重命名?

我的问题是关于在 Redis 的 c# ServiceStack 包装器中读取和组合数据的性能以及调用在内部如何工作。

我将解释两个有望产生最终结果的场景。一种情况是将类别 ID 列表附加到交易,以便类别可以独立存储。

问题:我的最终目标是检索类别为“食品”的所有交易。

我已尝试对其他有助于我理解的清晰点进行编号。假设有 10,000 笔交易,每笔交易平均有 3 个类别。

注意: ServiceStack.Net Redis: Storing Related Objects vs. Related Object Ids 有一个相关问题然而并没有解释效率。

示例 A

public class Transaction
{
    public List<string> CategoryIds;
}

示例 B

public class Transaction
{
    public List<string> CategoryNames;
}

代码

var transactionClient = redisClient.GetTypedClient<Transaction>();

//1. is this inefficient returning all transactions?
//   is there any filtering available at this part?
var allTransactions = transactionClient.GetAll();

//2. In the case of Example A where the categories are stored as id's
//   how would I map the categories to a transaction?
//   maybe I have a List that has a container with the Transaction associated with a
//   list of Categories, however this seems inefficient as I would have to loop 
//   through all transactions make a call to get their Categories and then 
//   populate the container datatype.

//3. If we are taking Example B how can I efficiently just retrieve the transactions
//   where they have a category of food.

最佳答案

效率是更少的网络调用更多的数据。 Redis 中的数据只是变得一团糟,大多数时候单个 API 调用与 redis 服务器操作 1:1 映射。这意味着您可以将 perf 影响视为简单地从远程服务器的内存中下载 json 数据集 blob 并在客户端对其进行反序列化 - 这实际上就是所有发生的事情。

在某些 API 中,例如 GetAll()它需要 2 次调用,1 次获取实体集中的所有 ID,另一个获取具有这些 ID 的所有记录。 Redis Client 的源代码非常平易近人,所以我建议您看看到底发生了什么。

因为您只有 3 个类别,所以尝试在服务器上进行过滤并不会节省多少额外数据。

所以你的选择基本上是:

  • 下载整个实体数据集并在客户端进行过滤
  • 维护来自类别 > ID 的自定义索引映射
  • 更高级:使用服务器端 LUA 操作来应用服务器端过滤(需要 Redis 2.6)

关于c# - ServiceStack Redis在检索数据时是如何发挥作用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11583080/

相关文章:

servicestack - 在 ServiceStack 中不使用 cookie 进行身份验证

c# - 如何避免子类属性在servicestack中序列化为json

c# - 获取密码时的字符串比较

JavaScript 到 C# 数值精度损失

redis - node-redis,如何获取哈希对象列表?

asp.net - 如何在 ASP.NET 4 Web 表单网站中利用 ServiceStack session /缓存?

c# - HttpWebRequest 调用页面两次

c# - 如何从客户端应用程序获取运行 Web 服务的服务器的 SSL 证书? - C#.NET

Redis 新手 - mysql 表的等效方法

java - Spring Boot Redis 将 POJO 列表存储为值