Class: Makara::Context
- Inherits:
-
Object
- Object
- Makara::Context
- Defined in:
- lib/makara/context.rb
Instance Attribute Summary collapse
-
#current_timestamp ⇒ Object
readonly
Returns the value of attribute current_timestamp.
-
#staged_data ⇒ Object
Returns the value of attribute staged_data.
-
#stored_data ⇒ Object
Returns the value of attribute stored_data.
Class Method Summary collapse
- .next ⇒ Object
- .release(proxy_id) ⇒ Object
- .release_all ⇒ Object
- .set_current(context_data) ⇒ Object
-
.stick(proxy_id, ttl) ⇒ Object
Called by ‘Proxy#stick_to_master!` to use master in subsequent requests.
- .stuck?(proxy_id) ⇒ Boolean
Instance Method Summary collapse
-
#commit ⇒ Object
Stores the staged data with an expiration time based on the current time, and clears any expired entries.
-
#initialize(context_data) ⇒ Context
constructor
A new instance of Context.
- #release(proxy_id) ⇒ Object
- #release_all ⇒ Object
- #stage(proxy_id, ttl) ⇒ Object
- #staged?(proxy_id) ⇒ Boolean
- #stuck?(proxy_id) ⇒ Boolean
Constructor Details
#initialize(context_data) ⇒ Context
Returns a new instance of Context.
9 10 11 12 13 14 15 |
# File 'lib/makara/context.rb', line 9 def initialize(context_data) @stored_data = context_data @staged_data = {} @dirty = @was_dirty = false freeze_time end |
Instance Attribute Details
#current_timestamp ⇒ Object (readonly)
Returns the value of attribute current_timestamp.
7 8 9 |
# File 'lib/makara/context.rb', line 7 def @current_timestamp end |
#staged_data ⇒ Object
Returns the value of attribute staged_data.
6 7 8 |
# File 'lib/makara/context.rb', line 6 def staged_data @staged_data end |
#stored_data ⇒ Object
Returns the value of attribute stored_data.
6 7 8 |
# File 'lib/makara/context.rb', line 6 def stored_data @stored_data end |
Class Method Details
.next ⇒ Object
111 112 113 114 115 |
# File 'lib/makara/context.rb', line 111 def next if current.commit current.stored_data end end |
.release(proxy_id) ⇒ Object
117 118 119 |
# File 'lib/makara/context.rb', line 117 def release(proxy_id) current.release(proxy_id) end |
.release_all ⇒ Object
121 122 123 |
# File 'lib/makara/context.rb', line 121 def release_all current.release_all end |
.set_current(context_data) ⇒ Object
98 99 100 |
# File 'lib/makara/context.rb', line 98 def set_current(context_data) set(:makara_current_context, new(context_data)) end |
.stick(proxy_id, ttl) ⇒ Object
Called by ‘Proxy#stick_to_master!` to use master in subsequent requests
103 104 105 |
# File 'lib/makara/context.rb', line 103 def stick(proxy_id, ttl) current.stage(proxy_id, ttl) end |
.stuck?(proxy_id) ⇒ Boolean
107 108 109 |
# File 'lib/makara/context.rb', line 107 def stuck?(proxy_id) current.staged?(proxy_id) || current.stuck?(proxy_id) end |
Instance Method Details
#commit ⇒ Object
Stores the staged data with an expiration time based on the current time, and clears any expired entries. Returns true if any changes were made to the current store
47 48 49 50 51 52 53 54 |
# File 'lib/makara/context.rb', line 47 def commit freeze_time release_expired store_staged_data clean was_dirty? end |
#release(proxy_id) ⇒ Object
29 30 31 32 |
# File 'lib/makara/context.rb', line 29 def release(proxy_id) @dirty ||= !!stored_data.delete(proxy_id) staged_data.delete(proxy_id) end |
#release_all ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/makara/context.rb', line 34 def release_all if self.stored_data.any? self.stored_data = {} # We need to track a change made to the current stored data # so we can commit it later @dirty = true end self.staged_data = {} end |
#stage(proxy_id, ttl) ⇒ Object
17 18 19 |
# File 'lib/makara/context.rb', line 17 def stage(proxy_id, ttl) staged_data[proxy_id] = [staged_data[proxy_id].to_f, ttl.to_f].max end |
#staged?(proxy_id) ⇒ Boolean
25 26 27 |
# File 'lib/makara/context.rb', line 25 def staged?(proxy_id) staged_data.key?(proxy_id) end |
#stuck?(proxy_id) ⇒ Boolean
21 22 23 |
# File 'lib/makara/context.rb', line 21 def stuck?(proxy_id) stored_data[proxy_id] && !expired?(stored_data[proxy_id]) end |