与 SQL Server 一起使用时如何定期刷新 dapper.net 缓存

发布于 2024-11-26 11:52:02 字数 317 浏览 2 评论 0原文

有人可以解释一下这意味着什么(来自 Dapper.net 网站)

限制和注意事项

Dapper 会缓存其运行的每个查询的信息,这使其能够快速具体化对象并快速处理参数。当前的实现将此信息缓存在 ConcurrentDictionary 对象中。它存储的对象永远不会被刷新。 如果您在不使用参数的情况下动态生成 SQL 字符串,则可能会遇到内存问题。我们可能会将字典转换为 LRU 缓存。

我无法理解粗体线的含义。我正在使用 SQL Server 和 C# 客户端。

有人可以提供一个会导致此内存问题的 C# 代码示例吗?谢谢

Can someone please explain what this means (from the Dapper.net website)

Limitations and caveats

Dapper caches information about every query it runs, this allow it to materialize objects quickly and process parameters quickly. The current implementation caches this information in a ConcurrentDictionary object. The objects it stores are never flushed. If you are generating SQL strings on the fly without using parameters it is possible you will hit memory issues. We may convert the dictionaries to an LRU Cache.

I am not able to understand what the line in bold means. I am using SQL Server and c# client.

Can someone please give a sample of c# code that will create this memory issue. thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌逼全场 2024-12-03 11:52:02

如果您在不使用参数的情况下动态生成 SQL 字符串,则可能会遇到内存问题。

你可以这样做:

cmd.CommandText = "SELECT email, passwd, login_id, full_name " + 
                  "FROM members " +
                  "WHERE email = '" + email + "'";

或者你可以这样做:

string s = "SELECT email, passwd, login_id, full_name " + 
           "FROM members WHERE " +
           "email = @email";
SqlCommand cmd = new SqlCommand(s);
cmd.Parameters.Add("@email", email);

后者是参数化的。它将被缓存一次。前者没有参数化。每次您使用不同的 email 值编写类似的查询时,它都会被缓存。这会爆炸你的记忆。

后者要优越得多。它避免了注入攻击。 dapper 可以缓存一次。 SQL Server将编译一次执行计划并将其缓存。

您应该(必须)已经使用参数化查询。如果你不是,那就放弃你正在做的一切,把它作为当务之急。

有人可以提供一个会导致此内存问题的 C# 代码示例吗?谢谢

只需循环执行前者即可。观察你的记忆力增长。循环执行后者。注意你的记忆力不要增长。

If you are generating SQL strings on the fly without using parameters it is possible you will hit memory issues.

You can do this:

cmd.CommandText = "SELECT email, passwd, login_id, full_name " + 
                  "FROM members " +
                  "WHERE email = '" + email + "'";

or you can do this:

string s = "SELECT email, passwd, login_id, full_name " + 
           "FROM members WHERE " +
           "email = @email";
SqlCommand cmd = new SqlCommand(s);
cmd.Parameters.Add("@email", email);

The latter is parameterized. It will be cached once. The former is not parameterized. It will be cached every time you write a query like it with a different value for email. This will explode your memory.

The latter is vastly superior. It avoids injection attacks. dapper can cache it once. SQL Server will compile the execution plan once and cache it.

You should (imperative) already be using parameterized queries. If you aren't, drop everything you are doing and make this an immediate priority.

Can someone please give a sample of c# code that will create this memory issue. thank you

Just do the former in a loop. Watch your memory grow. Do the latter in a loop. Watch your memory not grow.

浪荡不羁 2024-12-03 11:52:02

如果确实需要刷新缓存,可以调用 SqlMapper.PurgeQueryCache()

If you do need to flush the cache, you can invoke SqlMapper.PurgeQueryCache().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文