Class: Sunspot::SessionProxy::ThreadLocalSessionProxy

Inherits:
AbstractSessionProxy show all
Defined in:
lib/sunspot/session_proxy/thread_local_session_proxy.rb

Overview

This class implements a session proxy that creates a different Session object for each thread. Any multithreaded application should use this proxy.

Constant Summary collapse

FINALIZER =
Proc.new do |object_id|
  Thread.current[:"sunspot_session_#{object_id}"] = nil
end
@@next_id =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractSessionProxy

delegate, not_supported

Constructor Details

#initialize(config = Sunspot::Configuration.new) ⇒ ThreadLocalSessionProxy

Optionally pass an existing Sunspot::Configuration object. If none is passed, a default configuration is used; it can then be modified using the #config attribute.



27
28
29
30
# File 'lib/sunspot/session_proxy/thread_local_session_proxy.rb', line 27

def initialize(config = Sunspot::Configuration.new)
  @config = config
  ObjectSpace.define_finalizer(self, FINALIZER)
end

Instance Attribute Details

#configObject (readonly)

The configuration with which the thread-local sessions are initialized.



17
18
19
# File 'lib/sunspot/session_proxy/thread_local_session_proxy.rb', line 17

def config
  @config
end

Instance Method Details

#sessionObject

:nodoc:



32
33
34
# File 'lib/sunspot/session_proxy/thread_local_session_proxy.rb', line 32

def session #:nodoc:
  Thread.current[:"sunspot_session_#{object_id}"] ||= Session.new(config)
end