Class: RightScale::DispatchedCache
- Defined in:
- lib/right_agent/dispatched_cache.rb
Overview
Cache for requests that have been dispatched recently This cache is intended for use in checking for duplicate requests when there is only one server servicing a queue
Constant Summary collapse
- MAX_AGE =
Maximum number of seconds to retain a dispatched request in cache This must be greater than the maximum possible retry timeout to avoid duplicate execution of a request
12 * 60 * 60
Instance Method Summary collapse
-
#initialize(identity) ⇒ DispatchedCache
constructor
Initialize cache.
-
#serviced_by(token) ⇒ Object
Determine whether request has already been serviced.
-
#size ⇒ Object
Get local cache size.
-
#stats ⇒ Object
Get local cache statistics.
-
#store(token) ⇒ Object
Store dispatched request token in cache.
Constructor Details
#initialize(identity) ⇒ DispatchedCache
Initialize cache
Parameters
- identity(String)
-
Serialized identity of agent
39 40 41 42 43 44 |
# File 'lib/right_agent/dispatched_cache.rb', line 39 def initialize(identity) @identity = identity @cache = {} @lru = [] @max_age = MAX_AGE end |
Instance Method Details
#serviced_by(token) ⇒ Object
Determine whether request has already been serviced
Parameters
- token(String)
-
Generated message identifier
Return
- (String|nil)
-
Identity of agent that already serviced request, or nil if none
75 76 77 78 79 80 81 |
# File 'lib/right_agent/dispatched_cache.rb', line 75 def serviced_by(token) if @cache[token] @cache[token] = Time.now.to_i @lru.push(@lru.delete(token)) @identity end end |
#size ⇒ Object
Get local cache size
Return
- (Integer)
-
Number of cache entries
103 104 105 |
# File 'lib/right_agent/dispatched_cache.rb', line 103 def size @cache.size end |
#stats ⇒ Object
Get local cache statistics
Return
- stats(Hash|nil)
-
Current statistics, or nil if cache empty
- “local total”(Integer)
-
Total number in local cache, or nil if none
- “local max age”(String)
-
Time since oldest local cache entry created or updated
89 90 91 92 93 94 95 96 97 |
# File 'lib/right_agent/dispatched_cache.rb', line 89 def stats if (s = size) > 0 now = Time.now.to_i { "local total" => s, "local max age" => RightSupport::Stats.elapsed(now - @cache[@lru.first]) } end end |
#store(token) ⇒ Object
Store dispatched request token in cache
Parameters
- token(String)
-
Generated message identifier
Return
- true
-
Always return true
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/right_agent/dispatched_cache.rb', line 53 def store(token) if token now = Time.now.to_i if @cache.has_key?(token) @cache[token] = now @lru.push(@lru.delete(token)) else @cache[token] = now @lru.push(token) @cache.delete(@lru.shift) while (now - @cache[@lru.first]) > @max_age end end true end |