Class: ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
- Inherits:
-
Object
- Object
- ActiveRecord::Middleware::DatabaseSelector::Resolver::Session
- Defined in:
- lib/active_record/middleware/database_selector/resolver/session.rb
Overview
The session class is used by the DatabaseSelector::Resolver to save timestamps of the last write in the session.
The last_write is used to determine whether it’s safe to read from the replica or the request needs to be sent to the primary.
Instance Attribute Summary collapse
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Class Method Summary collapse
-
.call(request) ⇒ Object
:nodoc:.
-
.convert_time_to_timestamp(time) ⇒ Object
Converts time to a timestamp that represents milliseconds since epoch.
-
.convert_timestamp_to_time(timestamp) ⇒ Object
Converts milliseconds since epoch timestamp into a time object.
Instance Method Summary collapse
-
#initialize(session) ⇒ Session
constructor
A new instance of Session.
- #last_write_timestamp ⇒ Object
- #save(response) ⇒ Object
- #update_last_write_timestamp ⇒ Object
Constructor Details
#initialize(session) ⇒ Session
Returns a new instance of Session.
28 29 30 |
# File 'lib/active_record/middleware/database_selector/resolver/session.rb', line 28 def initialize(session) @session = session end |
Instance Attribute Details
#session ⇒ Object (readonly)
Returns the value of attribute session.
32 33 34 |
# File 'lib/active_record/middleware/database_selector/resolver/session.rb', line 32 def session @session end |
Class Method Details
.call(request) ⇒ Object
:nodoc:
13 14 15 |
# File 'lib/active_record/middleware/database_selector/resolver/session.rb', line 13 def self.call(request) new(request.session) end |
.convert_time_to_timestamp(time) ⇒ Object
Converts time to a timestamp that represents milliseconds since epoch.
19 20 21 |
# File 'lib/active_record/middleware/database_selector/resolver/session.rb', line 19 def self.(time) time.to_i * 1000 + time.usec / 1000 end |
.convert_timestamp_to_time(timestamp) ⇒ Object
Converts milliseconds since epoch timestamp into a time object.
24 25 26 |
# File 'lib/active_record/middleware/database_selector/resolver/session.rb', line 24 def self.() ? Time.at( / 1000, ( % 1000) * 1000) : Time.at(0) end |
Instance Method Details
#last_write_timestamp ⇒ Object
34 35 36 |
# File 'lib/active_record/middleware/database_selector/resolver/session.rb', line 34 def self.class.(session[:last_write]) end |
#save(response) ⇒ Object
42 43 |
# File 'lib/active_record/middleware/database_selector/resolver/session.rb', line 42 def save(response) end |
#update_last_write_timestamp ⇒ Object
38 39 40 |
# File 'lib/active_record/middleware/database_selector/resolver/session.rb', line 38 def session[:last_write] = self.class.(Time.now) end |