mongoid查询缓存
Rails 的 ActiveRecord 有一个称为查询缓存 (ActiveRecord::QueryCache) 的功能,它可以在请求的生命周期内保存 SQL 查询的结果。虽然我不太熟悉实现的内部结构,但我认为它将查询结果保存在 Rack env 中的某个位置,该结果在请求结束时被丢弃。
不幸的是,Mongoid 目前不提供此类功能,并且某些查询隐式发生(引用)这一事实加剧了这种情况。 我正在考虑实现这个功能,我很好奇,应该在哪里以及如何连接 Mongoid(或者也许是 mongo 驱动程序?)来实现这个功能。
Rails' ActiveRecord has a feature called Query Caching (ActiveRecord::QueryCache) which saves the result of SQL query for the life-span of a request. While I'm not very familiar with the internals of the implementation, I think that it saves the query results somewhere in the Rack env, which is discarded in the end of the request.
The Mongoid, unfortunately, doesn't currently provide such feature, and this is exacerbated by the fact, that some queries occur implicitly (references).
I'm considering to implement this feature, and I'm curious, where and how Mongoid (or, perhaps, mongo driver?) should be hooked in order to implement this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Mongoid 有缓存,如下所述 http://mongoid.org/en/mongoid/docs/ extras.html
另外MongoDB本身也有缓存能力: http://www.mongodb.org/display/DOCS/Caching
mongoid 缓存额外知道两种不同的情况:缓存模型的所有查询或缓存查询。
Mongoid 缓存的工作方式似乎略有不同:它看起来像 mongoid 委托缓存到 mongodb。 (在 mongoid 的源代码中,我只能找到缓存的选项设置,但找不到缓存模块。)
最后,我想说,一般来说,缓存没有真正的区别——在内存中实际上是在内存中!无论它是在应用程序中还是在数据库中。
我不喜欢实现额外的缓存算法,因为这似乎是多余的并且是 RAM 杀手。
顺便说一句:如果您确实想在应用程序内缓存结果,您可以尝试
Rails.cache
或其他缓存 gem 作为解决方法。Mongoid has caching, described under http://mongoid.org/en/mongoid/docs/extras.html
Also MongoDB itself has caching ability: http://www.mongodb.org/display/DOCS/Caching
The mongoid caching extra knows 2 different cases: Caching of all queries of a model or caching of a query.
Mongoid caching seems to work slightly differently: it looks like mongoid delegates caching to mongodb. (In the sources of mongoid I only can find option settings for caching but no cache module.)
Finally, I would say, there is no real difference in the caching in general -- in memory is in fact in memory! No matter if it's in the app or in the database.
I don't prefer to implement an extra caching algorithm, because this seems to be redundant and a RAM killer.
BTW: If your really want to cache results in-app you could try
Rails.cache
or another cache gem as a workaround.另一个答案显然是错误的。不仅 mongoid 或 mongo 驱动程序不会缓存查询,即使 mongo 会缓存查询 - 它仍然可能位于网络上的其他计算机上。
我的解决方案是将 receive_message 包装在 Mongo::Connection 中。
优点:有一个明确的位置
缺点:反序列化仍然发生
The other answer is obviously wrong. Not only mongoid or mongo driver doesn't cache the query, even if mongo would - it still might be on other machine across the network.
My solution was to wrap the receive_message in Mongo::Connection.
Pros: one definite place
Cons: deserialization still takes place
OK,Mongoid 4 支持 QueryCache 中间件。
只需在
application.rb
中添加中间件即可获利:
来源:
Mongoid 变更日志
https://github.com/mongoid/mongoid/blob/master/CHANGELOG.md#new-features-2
OK, Mongoid 4 supports QueryCache middleware.
Just add middleware in
application.rb
And then profit:
Source:
Mongoid changelog
https://github.com/mongoid/mongoid/blob/master/CHANGELOG.md#new-features-2
Mongoid 4.0+ 现在有一个 QueryCaching 模块: http://www.rubydoc.info/ github/mongoid/mongoid/Mongoid/QueryCache
您可以通过包装您的查找来在查找上使用它,如下所示:
Mongoid 4.0+ now has a QueryCaching module: http://www.rubydoc.info/github/mongoid/mongoid/Mongoid/QueryCache
You can use it on finds by wrapping your lookups like so: