我什么时候应该在 Rails 应用程序中创建 Solr 连接
我使用 rsolr (不是 Sunspot)在 Ruby on Rails 应用程序中访问 Solr。我创建了用于发送请求的本地 solr 对象,如下所示:
solr = RSolr.connect(:url => "http://localhost:8983/solr")
据我了解,这并不是真正的连接,而只是一个按需发出请求的对象,因此保持其初始化状态应该不会很昂贵它永远不应该断开。据此,拥有一个全局 solr 对象应该没问题,在启动时创建它并忘记它。正确的?但也许它不是线程安全的?
我应该什么时候创建 solr 连接?
I'm accessing Solr in a Ruby on Rails application by using rsolr (not Sunspot). I create the local solr object that I use to send requests like this:
solr = RSolr.connect(:url => "http://localhost:8983/solr")
as far as I understand, this is not really a connection but just an object that will issue requests on demand, so it shouldn't be expensive to keep it initialized and it should never disconnect. According to that, it should be ok to have one global solr object, create it at start time and forget about it. Right? But maybe it's not thread safe?
When should I create the solr connection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
RSolr.connect
方法真正做的就是清理并保存您正在使用的选项。您可以在此处看到该方法。它已通过一个新的连接对象(值得注意的是,它没有initialize
方法,因此在创建时它不会执行任何操作)以及传递给RSolr.connect
的选项。所以是的,你是对的——连接一次并让它永远挂在某个变量中并没有什么坏处。 (例如,我记住
RSolr.connect
在我的 Solr/Rails 应用程序中。)All that the
RSolr.connect
method really does is sanitize and save the options that you're using. You can see that method here. It's passed a new connection object (which, notably, doesn't have aninitialize
method, so it's not doing anything when created) and the options that you pass toRSolr.connect
.So yes, you're right -- no harm at all in connecting once and leaving it connected forever hanging around in a variable somewhere. (For example, I memoize the result of
RSolr.connect
in my Solr/Rails app.)