dapper 缓冲区/缓存说明 [英] Explanation of dapper buffer/cache

查看:33
本文介绍了dapper 缓冲区/缓存说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 dapper 从我的数据库中返回对象作为 IEnumerable.默认 dapper 的缓冲区设置设置为 true.

I use dapper to return objects from my database as IEnumerable. As default dapper has buffer setting set to true.

这是如何工作的?

如果 dapper 缓存第一个查询,然后从内存中获取对象.

If dapper cache the first query and then get the objects from memory.

如果有人编辑/删除/添加表格中的行会发生什么.必须为此查询重新缓存所有数据吗?

What happens if someone edit/delete/add rows in the table. Must dapper recache all data again for this query?

推荐答案

缓冲区与缓存无关.Dapper 不包括任何类型的数据缓存(尽管它确实有一个与它如何处理命令相关的缓存,即这个命令字符串,具有这种类型的参数和这种类型的实体 - 具有这些相关联的动态生成的方法来配置命令并填充对象").

The buffer is unrelated to cache. Dapper does not include any kind of data-cache (although it does have a cache related to how it processes commands, i.e. "this command string, with this type of parameter, and this type of entity - has these associated dynamically generated methods to configure the command and populate the objects").

这个开关的真正含义是:

What this switch really means is:

  • false:将在接收/使用项目时对其进行迭代 - 基本上,围绕 IDataReader 的迭代器块
    • 减:你只能迭代一次(除非你很乐意重新运行查询)
    • 另外:您可以迭代巨大查询(数百万行),而无需一次将它们全部存储在内存中 - 因为您只是真正查看正在生成的当前行
    • 另外:您无需等待数据结束即可开始迭代 - 只要它至少有一行,您就可以开始了
    • 减号:在您迭代时连接正在使用中,如果您尝试在一个每行(这可以通过 MARS 缓解)
    • 减:因为消费者可以对每个项目做任何他们想做的事情(每行可能需要几分钟,如果他们正在做一些复杂的事情),命令/阅读器可能会打开更长时间
    • false: will iterate items as they are recieved/consumed - basically, an iterator-block around an IDataReader
      • minus: you can only iterate it once (unless you are happy to re-run the query)
      • plus: you can iterate over immense queries (many millions of rows), without needing them all in-memory at once - since you're only ever really looking at the current row being yielded
      • plus: you don't need to wait for the end of the data to start iterating - as soon as it has at least one row, you're good to go
      • minus: the connection is in-use while you're iterating, which can lead to "there is already an open reader on the connection" (or whatever the exact wording is) errors if you try to invoke other commands on a per-row basis (this can be mitigated by MARS)
      • minus: because the consumer can do anything they want per-item (it could take minutes per row, if they are doing something complex), the command/reader might be open for longer
      • 加上:您可以根据需要多次迭代
      • 减:如果查询是巨大的,将它们全部加载到内存(在列表中)可能会很昂贵/不可能
      • 减:如果查询很大,在收集最后一行时可能会有明显的延迟
      • 加上:一旦你得到数据,命令就完成了——所以它和后续操作之间没有冲突
      • 加上:一旦您获得数据,该命令就已经释放了所有资源(锁等),因此您对服务器的影响最小

      大多数查询仅返回中等数量的数据(例如,少于 100 条记录),因此我们很高兴默认 (true) 为大多数情况提供最合适的行为.但我们为您提供了选项,以满足不同的使用场景.

      Most queries only return a moderate amount of data (say, less than 100 records), so we're happy that the default (true) gives the most appropriate behavior for most scenarios. But we make the option available to you, to cater for different usage scenarios.

      这篇关于dapper 缓冲区/缓存说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆