Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

.net - Explanation of dapper buffer/cache

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

How does this work?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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: 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
  • true (the default): the data is fully consumed into a List<T> before it hands it back to you
    • plus: you can iterate it as many times as you like
    • minus: if the query is immense, loading them all into memory (in a list) could be expensive / impossible
    • minus: if the query is large, there may be noticeable latency while it gathers the last row
    • plus: once you get the data, the command is complete - so there is no conflict between that and subsequent operations
    • plus: as soon as you get the data, the command has already released any resources (locks etc), so you're having minimal impact on the server

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...