且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

dapper 缓冲区/缓存说明

更新时间:2023-11-08 12:31:40

缓冲区与缓存无关.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.